1

I'm working to produce a facet/lattice plot of choropleth maps that each show a how different model runs affect one variable being mapped across a number of polygons. The problem is that the output graphic produces strange lines that run between the polygons in each plot (see the graphic below).

While I've manipulated and converted the shapefile into a data frame with appropriate attributes for ggplot2, I'm not familiar with the details of how to use the package and the online documentation is limited for such a complex package. I'm not sure what parameter is causing this issue, but I suspect it may be the aes parameter.

The script:

library(rgdal, tidyr, maptools, ggplot2, dplyr, reshape2)

setwd('D:/path/to/wd')

waterloo <- read.table("waterloo-data.txt", header=TRUE, sep=',', stringsAsFactors=FALSE)
waterloo <- data.frame(waterloo$DAUID, waterloo$LA0km, waterloo$LA4_exp, waterloo$LA20km, waterloo$LA30km, waterloo$LA40km, waterloo$LA50km)
colnames(waterloo) <- c("DAUID", "LA0km", "LA10km","LA20km", "LA30km", "LA40km", "LA50km")

## Produces expenditure measurements by ID variable DAUID, using reshape2/melt
wtidy <- melt(waterloo, id.vars=c("DAUID"), measure.vars = c("LA0km", "LA10km", "LA20km", "LA30km", "LA40km", "LA50km"))
colnames(wtidy) <- c("DAUID", "BufferSize", "Expenditure")

wtidy$DAUID <- as.factor(wtidy$DAUID) # for subsequent join with wtrl_f

### READ SPATIAL DATA ###
#wtrl <- readOGR(".", "Waterloo_DA_2011_new")
wtrl <- readShapeSpatial("Waterloo_DA_2011_new")
wtrl$id <- row.names(wtrl)

wtrl_f <- fortify(wtrl)
wtrl_f <- left_join(wtrl_f, wtrl@data, by="id")

# Join wtrl fortified (wtrl_f) to either twaterloo or wtidy
wtrl_f <- left_join(wtrl_f, wtidy, by="DAUID")

### PLOT SPATIAL DATA ###
ggplot(data = wtrl_f, # the input data
       aes(x = long.x, y = lat.x, fill = Variable/1000, group = BufferSize)) + # define variables
  geom_polygon() + # plot the DAs
  geom_path(colour="black", lwd=0.05) + # polygon borders
  coord_equal() + # fixed x and y scales
  facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size
  scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors
                       midpoint = 10000, name = "Variable\n(thousands)") + # legend options
  theme(axis.text = element_blank(), # change the theme options
        axis.title = element_blank(), # remove axis titles
        axis.ticks = element_blank()) # remove axis ticks

The output graphic appears as follows:

enter image description here Strange! I've made good progress but I don't know where ggplot is getting these lines. Any help on this would be appreciated!

PS; as an additional unrelated question, the polygon lines are rather jagged. How would I smooth these lines?

1
  • It would much easier to help if you could provide a minimal, reproducible example. Minimal meaning just enough code and data to show the problem. Reproducible meaning that I can paste your code exactly as provided into a fresh R session and run it without error (except for the error you need help with of course...)
    – bdemarest
    Feb 11, 2015 at 23:06

2 Answers 2

3

This answer helped me to solve my problem, but not before I made up this minimal example ready to post. I'm sharing it here in case it helps someone solve the same problem faster.

Problem: I'm trying to make a basic map in R with ggplot2. The polygons are filling wrong, making extra lines.

library("ggplot2")
library("maps")

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat)) + geom_polygon()
map

wrong map image

Solution: I have to set the aesthetic "group" parameter to put the polygon points in the right order, otherwise ggplot will try to plot a patch of Scotland coastline in the middle of the south coast (for example).

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat, group = group)) + geom_polygon()
map
0

OK, I managed to resolve this issue by changing the aesthetic group parameter found on page 11 of the ggplot2 manual: http://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf

The correct parameter is "group" and not the factor that is used to group the plots. The correct ggplot code:

    ggplot(data = wtrl_f, # the input data
       aes(x = long.x, y = lat.x, fill = Expenditure/1000, group = group)) + # define variables
  geom_polygon() + # plot the DAs
  geom_path(colour="black", lwd=0.025) + # DA borders
  coord_equal() + # fixed x and y scales
  facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size
  scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors
                       midpoint = 10000, name = "Expenditures\n(thousands)") + # legend options
  theme(axis.text = element_blank(), # change the theme options
        axis.title = element_blank(), # remove axis titles
        axis.ticks = element_blank()) # remove axis ticks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.