O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Plotting the crimes on the Chicago map

We can use the ggmap function to plot crime data points on the Chicago map using the latitude and longitude values provided for each crime activity:

> library(maps)> library(ggmap)> query= crime_collection$find('{}', fields = '{"_id":0, "Latitude":1, "Longitude":1,"Year":1}')> LatLonCounts=as.data.frame(table(round(query$Longitude,2), round(query$Latitude,2)))> # Convert the Longitude and Latitude variable to numbers:> LatLonCounts$Long = as.numeric(as.character(LatLonCounts$Var1))> LatLonCounts$Lat = as.numeric(as.character(LatLonCounts$Var2))> ggmap(chicago)+geom_tile(data = LatLonCounts, aes(x = Long, y = Lat, alpha = Freq), fill="red")+ggtitle("Crime Distribution")+labs(alpha="Count")+theme(plot.title ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required