In my previous post, I discussed how to use OpenStreetMaps (and standard plotting functions of R) to visualize John Snow’s dataset. But it is also possible to use Google Maps (and ggplot2 types of graphs).
library(ggmap) get_london <- get_map(c(-.137,51.513), zoom=17) london <- ggmap(get_london)
Again, the tricky part comes from the fact that the coordinate representation system, here, is not the same as the one used on Robin Wilson’s blog.
or, use d X_deaths.RData. So now, we have to change it
df_deaths <- data.frame(X) library(sp) library(rgdal) coordinates(df_deaths)=~coords.x1+coords.x2 proj4string(df_deaths)=CRS("+init=epsg:27700") df_deaths = spTransform(df_deaths,CRS("+proj=longlat +datum=WGS84"))
Here, we have the same coordinate system as the one used in Google Maps. Now, we can add a layer, with the points,
london + geom_point(aes(x=coords.x1, y=coords.x2),data=data.frame(df_deaths@coords),col="red")
Again, it is possible to add the density, as an additional layer,
london + geom_point(aes(x=coords.x1, y=coords.x2), data=data.frame(df_deaths@coords),col="red")+ geom_density2d(data = data.frame(df_deaths@coords), aes(x = coords.x1, y=coords.x2), size = 0.3) + stat_density2d(data = data.frame(df_deaths@coords), aes(x = coords.x1, y=coords.x2,fill = ..level.., alpha = ..level..),size = 0.01, bins = 16, geom = "polygon") + scale_fill_gradient(low = "green", high = "red",guide = FALSE) + scale_alpha(range = c(0, 0.3), guide = FALSE)
OpenEdition suggests that you cite this post as follows:
Arthur Charpentier (February 27, 2015). John Snow, and Google Maps. Freakonometrics. Retrieved October 8, 2024 from https://doi.org/10.58079/ouyo
Salut Arthur,
Juste une petite question technique. Pourrais-tu me dire avec quel système d’exploitation tu travailles : windows ? linux ? mac ? Et avec quelle version de R ? Je travaille usuellement avec la 3.0.2 et j’ai parfois des soucis pour installer des packages. Mais j’ai gardé une plus vielle au cas (2.15.3). Merci.
en fait, c’est vraiment le bordel ! je bosse à la fois au bureau sur une vieille version de R, sur windows, et sur mon laptop sur linux, avec une version plus récente. Et c’est effectivement l’horreur !
“deaths@coords”
Error in data.frame(deaths@coords) : trying to get slot “coords” from an object (class “data.frame”) that is not an S4 object
indeed…
it might be in one of the other “Cholera_Deaths” files… maybe the shp file ! I will check !
ok, you can find a X_deaths.RData file. If you load it, you will get an X object, with the coordinates
Thank you!
thank you for pointing out the mistake !