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

Find most severe crime zone

The aggregate() function allows you to run an aggregation pipeline. For example the following pipeline calculates the total crimes per location and then shows it using a horizontal bar plot which is shown in the graph after the code:

> library(ggplot2) 
> crime_collection$aggregate('[{"$group":{"_id":"$LocationDescription", "Count": {"$sum":1}}}]')%>%na.omit()%>%arrange(desc(Count))%>%head(10)%>%ggplot(aes(x=reorder(`_id`,Count),y=Count))+geom_bar(stat="identity",fill='#c37600')+geom_text(aes(label = Count), color = "blue") +coord_flip()+xlab("Location Description")

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