2016-04-22 84 views
1

我想制作纽约市犯罪发生的多个地块(以r为单位)。我想为每个分区制作地块,即78个。我的数据记录了7种不同类型的犯罪,我想用不同的颜色识别每种类型的犯罪。因此,对于每个情节(代表区),我会针对不同的犯罪有不同的颜色。我想在谷歌地球的卫星地图上绘制数据。下面是代码,当我试图使该地块我收到错误消息:ggplot2中的多个地块

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 

回答

0

我选择了两个Precinct此演示。你想用ggplot对象创建一个列表,绘制并保存地图。有关保存地图的选项,请参阅代码部分中的链接。 baptiste的答案将帮助您选择保存图像的最佳方式。我希望这能帮到您。

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

+0

嘿Jazzurro,非常感谢!有效。我非常非常感谢! – Stone

+0

@Stone我很高兴听到这个消息。你会考虑接受答案吗? – jazzurro