In pursuit of replicability (as well as requirements of funding agency), I am trying to produce all of the maps for a current project in R.

For single attribute maps, so far so good! I am using ggplot2 and enjoying all of the customization options.

However, I'm running into problems as to how I can make multiple attribute maps. In GIS terms, I'm interested in having one graduated color layer and one proportional symbol layer. Here is an example of the current code:

mapPlot <- ggplot(data) + theme_void() + theme(plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank() ,axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position="bottom", legend.key.width = unit(1, "cm"), legend.text = element_text(size = 8))


map <- mapPlot + geom_polygon(data=data, aes(x = long, y = lat, group = group, fill = data$var1), lwd = 1/10, color="lavenderblush4") + 
  scale_fill_continuous(name = "Average", low="white", high="dodgerblue4", guide="colorbar", limits=c(0.0,1.0), labels=c("6", "661", "1,106", "2,037", "11,846")) +
  theme(legend.text = element_text(size=10))

map <- map + coord_map(project="conic", lat0 = 30)

This is producing a nice graduated colors map, but I'm unsure how to combine it with an additional layer (and thus additional legend).

I would like to something (relatively) straightforward such as asked here: How do I plot points as graduated/proportional circles in R?

EDIT: Information about my data

-Currently using commuter zone areal units

-Do not have centroids for these units (yet - would they help?)

EDIT 2: Found this link (https://datacarpentry.org/r-raster-vector-geospatial/08-vector-plot-shapefiles-custom-legend/), going to update my results

Edit 3: No luck via link.


Maybe I'm misunderstanding the problem, but to plot two sets of data using two different aesthetics and hence get two different legends in ggplot is done like this:

First some data, points will do:

> d1 = data.frame(x=runif(10),y=runif(10),z=runif(10))
> d2 = data.frame(x=runif(10),y=runif(10),z=runif(10))
> library(ggplot2)

Now make a plot of points, one dataset with size and one with colour:

> ggplot() + 
    geom_point(data=d1, aes(x=x,y=y,size=z)) +
    geom_point(data=d2, aes(x=x,y=y,col=z))

enter image description here

That's a bit rubbish as a graphic since its hard to tell one set of points from the other, but if you are doing points over polygons you won't have that trouble. If you want to fix an attribute for a geom then put it outside the aesthetic function. For example, plot colour-scaled large diamonds and size-scaled default point circles:

 ggplot() + geom_point(data=d1, aes(x=x,y=y,size=z)) + geom_point(data=d2, aes(x=x,y=y,col=z),pch=18,size=10)
  • Thanks, @Spacedman! It seems I was too stuck in 'GIS' terms and not thinking enough in ggplot mindset.
    – user128912
    Mar 26 '19 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy