Merge legends on a map when data is in geometry

I use ggplot2

to plot the map and points on top of it. This consists of building a map and then using geom_point()

to place the points. In addition, these points differ in size based on another variable. When I paint this, it leads to several 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 (as well as this one ), this can usually be achieved using identical name

and labels

for both scales, however I am not sure how to apply this when no data is specified in the main call to ggplot (i.e. not in the geometry) and with size as legend. I've 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 merge the legends?

+3


source to share


1 answer


You get multiple legends because you have multiple arguments inside aes

. In your example the only aesthetics, which is dependent on the variable is size

, and the rest ( color

and alpha

) not to be displayed.

It:



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)

      

Gives you one legend already combined.

+1


source







All Articles