0

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

jazzurro
  • 23,179
  • 35
  • 66
  • 76
Pulsar_534511
  • 98
  • 1
  • 13
  • You can have one color legend, and one size legend. You can't easily have two size legends (one for each color). – Axeman Apr 06 '18 at 08:03
  • Also, i) you can't have two coordinate systems, ii) the `group` in `geom_point` isn't doing anything, iii) you probably want to scale size by area (`scale_size_area`) – Axeman Apr 06 '18 at 08:09

1 Answers1

1

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.

Axeman
  • 32,068
  • 8
  • 81
  • 94
hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • I think you want to avoid using your `shp` to draw a polygon. Your `shp` is the one for `geom_poin()`. Since there is no data for polygons, all you can do is to leave a graphic with data points, I think. – jazzurro Apr 06 '18 at 02:44
  • Thanks for your answer but I don't want to compromise with the size. I actually want only the upper legend in your answer i.e. two colors for obs and sim. Could you tell me how did you get it? – Pulsar_534511 Apr 06 '18 at 02:47
  • @Pulsar_534511, I added an explanation. This is actually a default behavior of `ggplot2` – hpesoj626 Apr 06 '18 at 02:51
  • okay, I was not aware of the 'gather' function! I see now how you got but still this is not what I want. – Pulsar_534511 Apr 06 '18 at 02:54
  • @Pulsar_534511, what exactly do you want? The figure in your link doesn't give different colors for the groups. – hpesoj626 Apr 06 '18 at 02:55
  • It does give different colors but both the legends are overlapped and they are transparent so it shows red+blue as purple color! What I want is two different legends showing red and blue color for sim and obs separately. – Pulsar_534511 Apr 06 '18 at 02:57
  • @hpesoj626 that link is for adding another legend giving the values of sim and obs! So basically I want three legend 1) for sim 2) for obs and 3) common legend giving the idea about the values of sim and obs. The third legend I can compromise with but two legends for sim and obs must be there in my plot. – Pulsar_534511 Apr 06 '18 at 03:05
  • Your question about color seems to have been addressed [here](https://stackoverflow.com/questions/8269742/in-r-is-it-possible-to-overlay-2-colours-in-a-plot-to-make-a-3rd-e-g-with-add) and [here](https://stackoverflow.com/questions/746899/how-to-calculate-an-rgb-colour-by-specifying-an-alpha-blending-amount). – hpesoj626 Apr 06 '18 at 03:21
  • Also, the breakdown of the plot that you linked to is in [here](https://flowingdata.com/2010/11/23/how-to-make-bubble-charts/). Good luck. – hpesoj626 Apr 06 '18 at 03:22
  • Thanks for your instant help. It worked to much extent what I wanted to do. Can you tell me how can I make the legend of size variable without color? It displays as gray color and if I chose "shape=21" then the bubbles are not filled with colors. – Pulsar_534511 Apr 07 '18 at 09:38
  • "*Can you tell me how can I make the legend of size variable without color? It displays as gray color and if I chose "shape=21" then the bubbles are not filled with colors.*" I don't think I understand that. – hpesoj626 Apr 07 '18 at 10:47