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: