Search code examples
rggplot2legendspatialbubble-chart

Custom legend for bubble plot using ggplot


I have a dataframe which contains a spatial variable with simulated and observed values.

df <- data.frame(sim = sample((20:30),10),
                 obs = sample(25:40,10),
                 long = rnorm(10,10,8),
                 lat = rnorm(10,30,15))

I have plotted a spatial bubble plot using the following code.

ggplot() +
geom_polygon(data = shp, aes(x = long, y = lat, group = group),
             col = "black", lwd = 0.8, fill = "slategray1",
             alpha = 0.5) +
coord_cartesian() + 
geom_point(data = df, aes(x = long, y = lat, group = sim, size = sim), col = "red", alpha = 0.5) +
geom_point(data = df, aes(x = long, y = lat, group = obs, size = obs), col = "blue", alpha = 0.5) +
scale_size_continuous(range = c(10,20)) +
coord_map(xlim = c(-1, 22), ylim = c(10, 45))

The output of the above code is in the image. What I want to show in the plot is the difference between the simulated and observed data which is done perfectly. Now, I want to show two different legend for the two variable sim and obs which will be red and blue color legend respectively. Also I want to show the magnitude of the variable in the legend but not as the varying size of bubbles, but in some different way. Something like given here. Can someone help me in achieving this?

enter image description here


Solution

  • Would it be okay with you to leave the size of the bubble as they are?

        df<-data.frame(sim=sample((20:30),10),obs=sample(25:40,10),long=rnorm(10,10,8),lat=rnorm(10,30,15))
    shp <- df %>%
      gather(group, value, -long, -lat)
    
    ggplot()+
      geom_polygon(data = df, aes(x = long, y = lat),col="black",lwd=0.8,fill="slategray1",alpha=0.5)+
      coord_cartesian()+
      geom_point(data=shp,aes(x=long,y=lat,group=group,size=value, fill = group, color = group),alpha=0.5)
    

    enter image description here

    Explanation

    To get different colors for the groups, gather obs and sim into one column and specify this column as the colour aesthetic. blue and red are the default colors ggplot2 assign for variables with two levels. So I deleted one geom_point and used the long form of your data instead for the remaining geom_point.