0

I'm new to R and I tried to solve the problem looking for other questions, but I coulndn't. I have a problem with overlapping different heatmaps I created using ggplot2 and ggmap. When plotting the maps singularly it works. Only when I try to plot them together the error :

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

appears.

This is my code:

us_map_g_str <- get_map(location = "detroit", zoom = 10)

ggmap(us_map_g_str, extent = "device") + 
  geom_density2d(data = data1, 
                 aes(x=as.numeric(lon), y = as.numeric(lat)), 
                 size = 0.3) + 
  stat_density2d(data = data1, 
                 aes(x = as.numeric(lon), y = as.numeric(lat),
                     fill = ..level.., alpha = ..level..),
                 size = 0.3, bins = 500, geom = "polygon") + 
  scale_fill_gradient(low = "green", high = "red") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE) + 
  geom_density2d(data = data2, 
                 aes(x = as.numeric(lon), y = as.numeric(lat)), 
                 size = 0.3) + 
  stat_density2d(data = data2, 
                 aes(x = as.numeric(lon), y = as.numeric(lat),
                     fill = ..level.., alpha = ..level..), 
                 size = 0.3, bins = 500, geom = "polygon") + 
  scale_fill_gradient(low = "blue", high = "black") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE) + 
  geom_density2d(data = data3, 
                 aes(x = as.numeric(lon), y = as.numeric(lat)), 
                 size = 0.3) + 
  stat_density2d(data = data3, 
                 aes(x = as.numeric(lon), y = as.numeric(lat),  
                     fill = ..level.., alpha = ..level..), 
                 size = 0.3, bins = 500, geom = "polygon") + 
  scale_fill_gradient(low = "yellow", high = "orange") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE)

When I run it, he applies to every heat map the the last colour, in this case yellow-orange.

This is what I get:

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
U.Cremona
  • 65
  • 10
  • 1
    That's expected behavior. There's one fill scale and one color scale per ggplot2 graphics. You _can_ combine grobs though, but it means working at the `grid` / `Grob` layer. There are some pointers here: http://stackoverflow.com/questions/16129876/ggplot2-multiple-scales-legends-per-aesthetic-revisited – hrbrmstr Sep 09 '15 at 10:39
  • I think it's much less complicated than that..there should be some sort of method which doesn't completely change the exisiting code – U.Cremona Sep 09 '15 at 10:56
  • The link you’ve posted points to your local computer. We can’t access that. – Konrad Rudolph Sep 09 '15 at 12:51
  • How do I post a plot I get on R here? – U.Cremona Sep 09 '15 at 13:05
  • ok I changed the link, it should work now – U.Cremona Sep 09 '15 at 13:20
  • 1
    @U.Cremona it's not less complicated unless you can create one large factor for every level of the fill then use `scale_fill_manual`, but then you'll have one, big, useless legend. One fill/color/size/etc scale per chart. Period. – hrbrmstr Sep 09 '15 at 14:48

0 Answers0