1

I want to make multiple plots (in r) of the occurrence of crime in a New York City. I want to make plots for each of the precincts i.e. 78. My data recorded seven different types of crime, and I want to identify each type of crime with a different color. So for each plot (representing a precinct) I would have different colors for different crime. I want to plot the data on a satellite map from google earth. Here is the code and the error message I received when I tried making the plots:

library(ggmap)
library(ggplot2)

FELONY <- read.csv("http://www.nyc.gov/html/nypd/downloads/excel/crime_statistics/Felony.csv")
felony <- FELONY[FELONY$OccurrenceYear==2015,]
attach(felony)
Sepfel <- felony[felony$OccurrenceMonth== "Sep",]

for(i in unique(Sepfel$Precinct)){
    map <- get_map(location='New York City', zoom=11, maptype="satellite")
    ggmap(map) + 
    geom_point(subset(Sepfel,Sepfel$Precint==i),
               aes(color=Offense, x=Longitude, y=Latitude),size=0.00001, alpha=1) +
    ggtitle(paste("September 2015"))
   }

Error: ggplot2 doesn't know how to deal with data of class uneval 
jazzurro
  • 23,179
  • 35
  • 66
  • 76
Stone
  • 7
  • 2

1 Answers1

0

I chose two Precinct for this demonstration. You want to create a list with ggplot objects, draw and save maps. For options to save maps, please see the link in the code part. baptiste's answer will help you to choose the best way for you to save your images. I hope this will help you.

foo <- subset(felony, Precinct %in% c("040", "045"))

mymap <- get_map(location = "New York City", zoom  = 11, maptype = "satellite")

mylist <- lapply(unique(foo$Precinct), function(x){

          g <- ggmap(mymap) + 
               geom_point(data = subset(foo, Precinct == x),
                          aes(color = Offense, x = Longitude, y = Latitude),
                          size = 0.5, alpha = 0.5)
               labs(title = "September 2015")

          g
        }
       )

### Credit to baptiste for this answer.
### http://stackoverflow.com/questions/20500706/saving-multiple-ggplots-from-ls-into-one-and-seperate-files-in-r

invisible(mapply(ggsave, file=paste0("Precinct-", unique(foo$Precinct), ".png"), plot = mylist))

enter image description here enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76