3

I am using ggplot2 to plot a map and points on top of it. This consists of plotting a map and then using geom_point() to place the points on. Furthermore these points vary in size based on another variable. When I plot this it results in multiple legends.

ggplot(mapdata, aes(x=long, y=lat)) + 
geom_map(map=mapdata, aes(map_id=region),  fill="#ffffff", color="black", size=0.15) +
xlim(4, 15) + 
ylim(47, 55) +
geom_point(data = dat, aes(long, lat, size = feature, alpha = 0.2, colour = "#007f3f")) +
scale_colour_manual(values = "#007f3f") 

enter image description here

As explained in this question (and also this one) this can usually be achieved by using identical name and labels values for both scales however I am unsure how to apply this when the data is not being specified in the primary ggplot call (i.e. not in a geom) and with size as the legend. I have tried many iterations of this:

ggplot(mapdata, aes(x=long, y=lat)) + 
  geom_map(map=mapdata, aes(map_id=region),  fill="#ffffff", color="black", size=0.15) +
  xlim(4, 15) + ylim(47, 55) +
  geom_point(data = dat, aes(long, lat, size = feature, alpha = 0.2, colour = "#007f3f")) +
  scale_color_manual(name = "feature", values = "#007f3f", labels=c("1.0", "1.5", "2.0", "2.5", "3.0")) +
  scale_size_manual(name = "feature", values = c(1.0, 1.5, 2.0, 2.5, 3.0), labels=c("1.0", "1.5", "2.0", "2.5", "3.0"))

How can I combine the legends?

G_T
  • 1,555
  • 1
  • 18
  • 34

1 Answers1

1

You get multiple legends because you have multiple arguments inside aes. In your example the only aesthetic that varies based on a variable is the size, the rest (color and alpha) do not need to be mapped.

This:

ggplot(mapdata, aes(long, lat)) + 
    geom_map(map = mapdata, 
             aes(map_id = region),
             fill="#ffffff",
             color="black",
             size=0.15) +
    geom_point(data = dat, 
               aes(long, lat, size = feature), 
               alpha = 0.2, 
               colour = "#007f3f")+
    xlim(4, 15) + 
    ylim(47, 55)

Will give you a single legend, already combined.

GGamba
  • 13,140
  • 3
  • 38
  • 47