<p>MongoDB is a NoSQL database program which uses JSON-like documents with schemas. It is free and open-source cross-platform database. MongoDB, top NoSQL database engine in use today, could be a good data storage alternative when analyzing large volume data.<br>
To use MongoDB with R, first, we have to download and install <a target="_blank" rel="nofollow" href="https://docs.mongodb.com/manual/installation/">MongoDB</a><br>
Next, start MongoDB. We can start MongoDB like so:</p>
<pre>mongod</pre>
<p>We can use the mongolite, package which is a fast and simple MongoDB client for R, to use MongoDB with R. </p>
<h2>Inserting data</h2>
<p>Let’s insert the crimes data from <a target="_blank" rel="nofollow" href="https://catalog.data.gov/dataset/crimes-2001-to-present-398a4">data.gov</a> to MongoDB. The dataset reflects reported incidents of crime (with the exception of murders where data exists for each victim) that occurred in the City of Chicago since 2001.</p>
<pre>
library(ggplot2)
library(dplyr)
library(maps)
library(ggmap)
library(mongolite)
library(lubridate)
library(gridExtra)

crimes=data.table::fread(&quot;Crimes_2001_to_present.csv&quot;)
names(crimes)
<em>'ID' 'Case Number' 'Date' 'Block' 'IUCR' 'Primary Type' 'Description' 'Location Description' 'Arrest'
'Domestic' 'Beat' 'District' 'Ward' 'Community Area' 'FBI Code' 'X Coordinate' 'Y Coordinate' 'Year' 'Updated On' 'Latitude' 'Longitude' 'Location'</em></pre>
<p>Let’s remove spaces in the column names to avoid any problems when we query it from MongoDB.</p>
<pre>names(crimes) = gsub(&quot; &quot;,&quot;&quot;,names(crimes)) 
names(crimes)
<em>'ID' 'CaseNumber' 'Date' 'Block' 'IUCR' 'PrimaryType' 'Description' 'LocationDescription' 'Arrest' 'Domestic' 'Beat' 'District' 'Ward' 'CommunityArea' 'FBICode' 'XCoordinate' 'YCoordinate' 'Year' 'UpdatedOn' 'Latitude' 'Longitude' 'Location'</em></pre>
<p>we can use the insert function from the mongolite package to insert rows to a collection in MongoDB.<br>
Let’s create a database called Chicago and call the collection crimes.</p>
<pre>
my_collection = mongo(collection = &quot;crimes&quot;, db = &quot;Chicago&quot;) # create connection, database and collection
my_collection$insert(crimes)
</pre>
<p>Let’s check if we have inserted the “crimes” data.</p>
<pre>my_collection$count()
<em>6261148 </em></pre>
<p>We see that the collection has 6261148 records.</p>
<h2>Performing a query and retrieving data</h2>
<p>First, let’s look what the data looks like by displaying one record:</p>
<pre>my_collection$iterate()$one()
<em>$ID
1454164
$CaseNumber
'G185744'
$Date
'04/01/2001 06:00:00 PM'
$Block
'049XX N MENARD AV'
$IUCR
'0910'
$PrimaryType
'MOTOR VEHICLE THEFT'
$Description
'AUTOMOBILE'
$LocationDescription
'STREET'
$Arrest
'false'
$Domestic
'false'
$Beat
1622
$District
16
$FBICode
'07'
$XCoordinate
1136545
$YCoordinate
1932203
$Year
2001
$UpdatedOn
'08/17/2015 03:03:40 PM'
$Latitude
41.970129962
$Longitude
-87.773302309
$Location
'(41.970129962, -87.773302309)'</em>
</pre>
<p>How many distinct “Primary Type” do we have?</p>
<pre>
length(my_collection$distinct(&quot;PrimaryType&quot;))
<em>35</em></pre>
<p>As shown above, there are 35 different crime primary types in the database. We will see the patterns of the most common crime types below.<br>
From the <em>iterate()iterate() $one()</em> command above, we have seen that one of the columns is Domestic, which shows whether the crime is domestic or not. </p>
<h3>Now, let’s see how many domestic assualts there are in the collection.</h3>
<pre>my_collection$count('{&quot;PrimaryType&quot; : &quot;ASSAULT&quot;, &quot;Domestic&quot; : &quot;true&quot; }')
<em>82470
</em>
</pre>
<p>To get the filtered data and we can also retrieve only the columns of interest</p>
<pre>
query1= my_collection$find('{&quot;PrimaryType&quot; : &quot;ASSAULT&quot;, &quot;Domestic&quot; : &quot;true&quot; }')
query2= my_collection$find('{&quot;PrimaryType&quot; : &quot;ASSAULT&quot;, &quot;Domestic&quot; : &quot;true&quot; }',
                           fields = '{&quot;_id&quot;:0, &quot;PrimaryType&quot;:1, &quot;Domestic&quot;:1}')
ncol(query1) # with all the columns
ncol(query2) # only the selected columns
<em>22
2
</em></pre>
<h2>Where do most crimes take pace?</h2>
<pre>
my_collection$aggregate('[{&quot;$group&quot;:{&quot;_id&quot;:&quot;$LocationDescription&quot;, &quot;Count&quot;: {&quot;$sum&quot;:1}}}]')%>%na.omit()%>%
arrange(desc(Count))%>%head(10)%>%
ggplot(aes(x=reorder(`_id`,Count),y=Count))+
geom_bar(stat=&quot;identity&quot;,color='skyblue',fill='#b35900')+geom_text(aes(label = Count), color = &quot;blue&quot;) +coord_flip()+xlab(&quot;Location Description&quot;)
</pre>
<p><a title="" data-rel="lightbox-0" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/1-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5693" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/1-1-490x245.png?resize=456%2C228&ssl=1" data-recalc-dims="1"></a><br>
If loading the entire dataset we are working with does not slow down our analysis, we can use data.table or dplyr but when dealing with big data, using MongoDB can give us performance boost as the whole data will not be loaded into mememory. We can reproduce the above plot without using MongoDB, like so:</p>
<pre>crimes%>%group_by(`LocationDescription`)%>%summarise(Total=n())%>% arrange(desc(Total))%>%head(10)%>%
ggplot(aes(x=reorder(`LocationDescription`,Total),y=Total))+
geom_bar(stat=&quot;identity&quot;,color='skyblue',fill='#b35900')+geom_text(aes(label = Total), color = &quot;blue&quot;) +coord_flip()+xlab(&quot;Location Description&quot;)</pre>
<p><a title="" data-rel="lightbox-1" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/2-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5694" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/2-1-490x245.png?resize=456%2C228&ssl=1" data-recalc-dims="1"></a></p>
<p>What if we want to query all records for certain columns only? This helps us to load only the columns we want and to save memory for our analysis.</p>
<pre>
query3= my_collection$find('{}', fields = '{&quot;_id&quot;:0, &quot;Latitude&quot;:1, &quot;Longitude&quot;:1,&quot;Year&quot;:1}')
</pre>
<h3>Let’s explore domestic crimes</h3>
<p>We can explore any patterns of domestic crimes. For example, are they common in certain days/hours/months?</p>
<pre>
domestic=my_collection$find('{&quot;Domestic&quot;:&quot;true&quot;}', fields = '{&quot;_id&quot;:0, &quot;Domestic&quot;:1,&quot;Date&quot;:1}')
domestic$Date= mdy_hms(domestic$Date)
domestic$Weekday = weekdays(domestic$Date)
domestic$Hour = hour(domestic$Date)
domestic$month = month(domestic$Date,label=TRUE)

WeekdayCounts = as.data.frame(table(domestic$Weekday))
WeekdayCounts$Var1 = factor(WeekdayCounts$Var1, ordered=TRUE, levels=c(&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;,&quot;Saturday&quot;))
ggplot(WeekdayCounts, aes(x=Var1, y=Freq)) + geom_line(aes(group=1),size=2,color=&quot;red&quot;) + xlab(&quot;Day of the Week&quot;) + ylab(&quot;Total Domestic Crimes&quot;)+
ggtitle(&quot;Domestic Crimes in the City of Chicago Since 2001&quot;)+
theme(axis.title.x=element_blank(),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
      axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
     axis.title.y = element_text(size=14),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-2" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/3-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5695" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/3-1-490x245.png?resize=456%2C228&ssl=1" data-recalc-dims="1"></a></p>
<p>Domestic crimes are common over the weekend than in weekdays? What could be the reason?<br>
We can also see the pattern for each day by hour:</p>
<pre>
DayHourCounts = as.data.frame(table(domestic$Weekday, domestic$Hour))
DayHourCounts$Hour = as.numeric(as.character(DayHourCounts$Var2))
ggplot(DayHourCounts, aes(x=Hour, y=Freq)) + geom_line(aes(group=Var1, color=Var1), size=1.4)+ylab(&quot;Count&quot;)+
ylab(&quot;Total Domestic Crimes&quot;)+ggtitle(&quot;Domestic Crimes in the City of Chicago Since 2001&quot;)+
theme(axis.title.x=element_text(size=14),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
      axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
     axis.title.y = element_text(size=14),
     legend.title=element_blank(),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-3" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/4-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5696" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/4-1-490x294.png?resize=456%2C274&ssl=1" data-recalc-dims="1"></a><br>
The crimes peak mainly around mid-night. We can also use one color for weekdays and another color for weekend as shown below.</p>
<pre>
DayHourCounts$Type = ifelse((DayHourCounts$Var1 == &quot;Sunday&quot;) | (DayHourCounts$Var1 == &quot;Saturday&quot;), &quot;Weekend&quot;, &quot;Weekday&quot;)

ggplot(DayHourCounts, aes(x=Hour, y=Freq)) + geom_line(aes(group=Var1, color=Type), size=2, alpha=0.5) +
ylab(&quot;Total Domestic Crimes&quot;)+ggtitle(&quot;Domestic Crimes in the City of Chicago Since 2001&quot;)+
theme(axis.title.x=element_text(size=14),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
      axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
     axis.title.y = element_text(size=14),
     legend.title=element_blank(),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-4" target="_blank" rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/5-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5697" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/5-1-490x294.png?resize=456%2C274&ssl=1" data-recalc-dims="1"></a><br>
The difference between weekend and weekdays are more clear from this figure than from the previous plot. We can also see the above pattern from a heatmap.</p>
<pre>
DayHourCounts$Var1 = factor(DayHourCounts$Var1, ordered=TRUE, levels=c(&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;, &quot;Sunday&quot;))
ggplot(DayHourCounts, aes(x = Hour, y = Var1)) + geom_tile(aes(fill = Freq)) + scale_fill_gradient(name=&quot;Total MV Thefts&quot;, low=&quot;white&quot;, high=&quot;red&quot;) +
ggtitle(&quot;Domestic Crimes in the City of Chicago Since 2001&quot;)+theme(axis.title.y = element_blank())+ylab(&quot;&quot;)+
theme(axis.title.x=element_text(size=14),axis.text.y = element_text(size=13),
      axis.text.x = element_text(size=13),
     axis.title.y = element_text(size=14),
     legend.title=element_blank(),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-5" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/6-1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5698" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/6-1-490x294.png?resize=456%2C274&ssl=1" data-recalc-dims="1"></a><br>
From the heatmap, we can see more crimes over weekends and at night.</p>
<pre>
monthCounts = as.data.frame(table(domestic$month))
ggplot(monthCounts, aes(x=Var1, y=Freq)) + geom_line(aes(group=1),size=2,color=&quot;red&quot;) + xlab(&quot;Day of the Week&quot;) + ylab(&quot;Total Domestic Crimes&quot;)+
ggtitle(&quot;Domestic Crimes in the City of Chicago Since 2001&quot;)+
theme(axis.title.x=element_blank(),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
      axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
     axis.title.y = element_text(size=14),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-6" target="_blank" rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/7.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5699" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/7-490x245.png?resize=456%2C228&ssl=1" data-recalc-dims="1"></a><br>
Is domestic crime associated with temperature? Domestic crimes tremendously increases during the warm months.</p>
<p>Now, let’s see the pattern of other crime types. Since there are 35 primary types, we cannot see all of them in this post. Let’s focus on four of the most common ones.</p>
<pre>
crimes=my_collection$find('{}', fields = '{&quot;_id&quot;:0, &quot;PrimaryType&quot;:1,&quot;Year&quot;:1}')
crimes%>%group_by(PrimaryType)%>%summarize(Count=n())%>%arrange(desc(Count))%>%head(4)
<em>Imported 6261148 records. Simplifying into dataframe...
PrimaryType	Count
THEFT	1301434
BATTERY	1142377
CRIMINAL DAMAGE	720143
NARCOTICS	687790
</em>
</pre>
<p>As shown in the table above, the most common crime type is theft followed by battery. Narcotics is fourth most common while criminal damage is the third most common crime type in the city of Chicago.<br>
Now, let’s generate plots by day and hour.</p>
<pre>
crimes=my_collection$find('{}', fields = '{&quot;_id&quot;:0, &quot;PrimaryType&quot;:1,&quot;Date&quot;:1}')
crimes=filter(crimes,PrimaryType %in%four_most_common)
crimes$Date= mdy_hms(crimes$Date)
crimes$Weekday = weekdays(crimes$Date)
crimes$Hour = hour(crimes$Date)
crimes$month=month(crimes$Date,label = TRUE)

g = function(data){
    WeekdayCounts = as.data.frame(table(data$Weekday))
    WeekdayCounts$Var1 = factor(WeekdayCounts$Var1, ordered=TRUE, levels=c(&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;,&quot;Saturday&quot;))
    ggplot(WeekdayCounts, aes(x=Var1, y=Freq)) + geom_line(aes(group=1),size=2,color=&quot;red&quot;) + xlab(&quot;Day of the Week&quot;) +
    theme(axis.title.x=element_blank(),axis.text.y = element_text(color=&quot;blue&quot;,size=10,angle=0,hjust=1,vjust=0),
          axis.text.x = element_text(color=&quot;blue&quot;,size=10,angle=0,hjust=.5,vjust=.5),
         axis.title.y = element_text(size=11),
         plot.title=element_text(size=12,color=&quot;purple&quot;,hjust=0.5))
    }

g1=g(filter(crimes,PrimaryType==&quot;THEFT&quot;))+ggtitle(&quot;Theft&quot;)+ylab(&quot;Total Count&quot;)
g2=g(filter(crimes,PrimaryType==&quot;BATTERY&quot;))+ggtitle(&quot;BATTERY&quot;)+ylab(&quot;Total Count&quot;)
g3=g(filter(crimes,PrimaryType==&quot;CRIMINAL DAMAGE&quot;))+ggtitle(&quot;CRIMINAL DAMAGE&quot;)+ylab(&quot;Total Count&quot;)
g4=g(filter(crimes,PrimaryType==&quot;NARCOTICS&quot;))+ggtitle(&quot;NARCOTICS&quot;)+ylab(&quot;Total Count&quot;)
grid.arrange(g1,g2,g3,g4,ncol=2)
</pre>
<p><a title="" data-rel="lightbox-7" target="_blank" rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/8.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5700" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/8-490x327.png?resize=456%2C304&ssl=1" data-recalc-dims="1"></a></p>
<p>From the plots above, we see that theft is most common on Friday. Battery and criminal damage, on the other hand, are highest at weekend. We also observe that narcotics decreases over weekend.<br>
We can also see the pattern of the above four crime types by hour:</p>
<pre>
g=function(data){
    DayHourCounts = as.data.frame(table(data$Weekday, data$Hour))
    DayHourCounts$Hour = as.numeric(as.character(DayHourCounts$Var2))
    ggplot(DayHourCounts, aes(x=Hour, y=Freq)) + geom_line(aes(group=Var1, color=Var1), size=1.4)+ylab(&quot;Count&quot;)+
    theme(axis.title.x=element_text(size=14),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
      axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
     axis.title.y = element_text(size=14),
     legend.title=element_blank(),
     plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
    }

g1=g(filter(crimes,PrimaryType==&quot;THEFT&quot;))+ggtitle(&quot;Theft&quot;)+ylab(&quot;Total Count&quot;)
g2=g(filter(crimes,PrimaryType==&quot;BATTERY&quot;))+ggtitle(&quot;BATTERY&quot;)+ylab(&quot;Total Count&quot;)
g3=g(filter(crimes,PrimaryType==&quot;CRIMINAL DAMAGE&quot;))+ggtitle(&quot;CRIMINAL DAMAGE&quot;)+ylab(&quot;Total Count&quot;)
g4=g(filter(crimes,PrimaryType==&quot;NARCOTICS&quot;))+ggtitle(&quot;NARCOTICS&quot;)+ylab(&quot;Total Count&quot;)
grid.arrange(g1,g2,g3,g4,ncol=2)
</pre>
<p><a title="" data-rel="lightbox-8" target="_blank" rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/9.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5701" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/9-490x245.png?resize=456%2C228&ssl=1" data-recalc-dims="1"></a></p>
<pre>
g=function(data){
    monthCounts = as.data.frame(table(data$month))
    ggplot(monthCounts, aes(x=Var1, y=Freq)) + geom_line(aes(group=1),size=2,color=&quot;red&quot;) + xlab(&quot;Day of the Week&quot;) + 
    theme(axis.title.x=element_blank(),axis.text.y = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=1,vjust=0),
          axis.text.x = element_text(color=&quot;blue&quot;,size=11,angle=0,hjust=.5,vjust=.5),
         axis.title.y = element_text(size=14),
         plot.title=element_text(size=16,color=&quot;purple&quot;,hjust=0.5))
    }

g1=g(filter(crimes,PrimaryType==&quot;THEFT&quot;))+ggtitle(&quot;Theft&quot;)+ylab(&quot;Total Count&quot;)
g2=g(filter(crimes,PrimaryType==&quot;BATTERY&quot;))+ggtitle(&quot;BATTERY&quot;)+ylab(&quot;Total Count&quot;)
g3=g(filter(crimes,PrimaryType==&quot;CRIMINAL DAMAGE&quot;))+ggtitle(&quot;CRIMINAL DAMAGE&quot;)+ylab(&quot;Total Count&quot;)
g4=g(filter(crimes,PrimaryType==&quot;NARCOTICS&quot;))+ggtitle(&quot;NARCOTICS&quot;)+ylab(&quot;Total Count&quot;)
grid.arrange(g1,g2,g3,g4,ncol=2)
</pre>
<p><a title="" data-rel="lightbox-9" target="_blank" rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/11.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5703" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/11-490x327.png?resize=456%2C304&ssl=1" data-recalc-dims="1"></a></p>
<p>Except, narcotics, all increase in the warmer months. Does this have any association with temperature?</p>
<h3>We can also produce maps.</h3>
<pre>
chicago = get_map(location = &quot;chicago&quot;, zoom = 11) # Load a map of Chicago into R:
</pre>
<p>Round our latitude and longitude to 2 digits of accuracy, and create a crime counts data frame for each area:</p>
<pre>
query3= my_collection$find('{}', fields = '{&quot;_id&quot;:0, &quot;Latitude&quot;:1, &quot;Longitude&quot;:1,&quot;Year&quot;:1}')
LatLonCounts=as.data.frame(table(round(query3$Longitude,2),round(query3$Latitude,2)))
</pre>
<p>Convert our Longitude and Latitude variable to numbers:</p>
<pre>
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=&quot;red&quot;)+
ggtitle(&quot;Crime Distribution&quot;)+labs(alpha=&quot;Count&quot;)+theme(plot.title = element_text(hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-10" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/12.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5704" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/12-490x368.png?resize=456%2C342&ssl=1" data-recalc-dims="1"></a></p>
<p>We can also see a map for domestic crimes only:</p>
<pre>
domestic=my_collection$find('{&quot;Domestic&quot;:&quot;true&quot;}', fields = '{&quot;_id&quot;:0, &quot;Latitude&quot;:1, &quot;Longitude&quot;:1,&quot;Year&quot;:1}')
LatLonCounts=as.data.frame(table(round(domestic$Longitude,2),round(domestic$Latitude,2)))
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=&quot;red&quot;)+
ggtitle(&quot;Domestic Crimes&quot;)+labs(alpha=&quot;Count&quot;)+theme(plot.title = element_text(hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-11" target="_blank" rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/13.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5705" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/13-490x368.png?resize=456%2C342&ssl=1" data-recalc-dims="1"></a><br>
Let’s see where motor vehicle theft is common:</p>
<pre>
mtheft=my_collection$find('{&quot;PrimaryType&quot;:&quot;MOTOR VEHICLE THEFT&quot;}', fields = '{&quot;_id&quot;:0, &quot;Latitude&quot;:1, &quot;Longitude&quot;:1,&quot;Year&quot;:1}')
LatLonCounts=as.data.frame(table(round(mtheft$Longitude,2),round(mtheft$Latitude,2)))
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=&quot;red&quot;)+
ggtitle(&quot;Motor Vehicle Theft &quot;)+labs(alpha=&quot;Count&quot;)+theme(plot.title = element_text(hjust=0.5))
</pre>
<p><a title="" data-rel="lightbox-12" target="_blank" rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/02/14.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-5706" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/02/14-490x368.png?resize=456%2C342&ssl=1" data-recalc-dims="1"></a><br>
Domestic crimes show concentration over two areas whereas motor vehicle theft is wide spread over large part of the city of Chicago.</p>
<h2>Summary</h2>
<p>In this post, we saw how to use R to analyse data stored in MongoDB, top NoSQL database engine in use today. When dealing with large volume data, using MongoDB can give us performance boost and make our life happier <img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s.w.org/images/core/emoji/2.2.1/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"></p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toFinding Optimal Number of Clusters" target="_blank" rel="nofollow" href="https://datascienceplus.com/finding-optimal-number-of-clusters/">Finding Optimal Number of Clusters</a></li>
<li><a title="Permanent Link toAnalyzing the first Presidential Debate" target="_blank" rel="nofollow" href="https://datascienceplus.com/analyzing-the-first-presidential-debate/">Analyzing the first Presidential Debate</a></li>
<li><a title="Permanent Link toGoodReads: Machine Learning (Part 3)" target="_blank" rel="nofollow" href="https://datascienceplus.com/goodreads-machine-learning-part-3/">GoodReads: Machine Learning (Part 3)</a></li>
<li><a title="Permanent Link toMachine Learning for Drug Adverse Event Discovery" target="_blank" rel="nofollow" href="https://datascienceplus.com/machine-learning-for-drug-adverse-event-discovery/">Machine Learning for Drug Adverse Event Discovery</a></li>
<li><a title="Permanent Link toGoodReads: Exploratory data analysis and sentiment analysis (Part 2)" target="_blank" rel="nofollow" href="https://datascienceplus.com/exploratory-data-analysis-and-sentiment-analysis/">GoodReads: Exploratory data analysis and sentiment analysis (Part 2)</a></li>
</ol></strong>
<div>
<a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=3QUf8RkvZkI:WaNO3-HpHZU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=3QUf8RkvZkI:WaNO3-HpHZU:D7DqB2pKExk" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=3QUf8RkvZkI:WaNO3-HpHZU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=3QUf8RkvZkI:WaNO3-HpHZU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6J84WBTGUhM">