[R-sig-Geo] Density on worldmap

Nico Hahn nico.hahn at yahoo.de
Tue Mar 20 15:01:45 CET 2018


Dear all,

I'm trying to plot a density on a worldmap using ggplot2.  I have three variables: Freq, Latitude and Longitude.  However most of my points are around the same coordinates, which is why my density is displayed red there. Is there a way that the displayed density dependts on the variable Freq instead of the amount of points in the area?

My current code:
rel = as.data.frame(matrix(0, nrow = 6, ncol = 3))
rel[,1] = c(1,0,0,0.73,0.94,1)
rel[,2] = c(34.55040,34.16807,36.02210,36.09917,36.35868,32.12694)
rel[,3] = c(40.88923, 45.10331, 43.40581, 43.32750, 43.15194, 45.23083)
colnames(rel) = c("Freq", "Latitude", "Longitude")

map = get_map(location = c(lon=43, lat=36), zoom = 6)
ggmap(map) + 
  geom_density2d(aes(x=Longitude, y=Latitude), 
                 data =  rel ,size = 0.3) +
  stat_density2d(data = rel, aes(x=Longitude, y=Latitude,
                                fill = ..level.., 
                                 alpha = ..level..), size =0.01, bins = 16,
                 geom = "polygon") + 
  scale_fill_gradient(low = "green", high = "red") +
  scale_alpha(range = c(0, 0.3), guide = FALSE) +
  geom_point(aes(x=Longitude, y=Latitude, colour = Freq),alpha = 0.75, size=5, data = rel)+
scale_colour_gradient(low = "#3afff4", high = "#fc0000") 

I would like the density to be displayed in red everywhere where red points are present.

Thanks,
Nico

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list