简体   繁体   English

ggplot2中的多个图

[英]Multiple plots in ggplot2

I want to make multiple plots (in r) of the occurrence of crime in a New York City. 我想在纽约市制作犯罪事件的多个情节(在r中)。 I want to make plots for each of the precincts ie 78. My data recorded seven different types of crime, and I want to identify each type of crime with a different color. 我想为每个区域制作情节,即78.我的数据记录了七种不同类型的犯罪,我想用不同的颜色识别每种类型的犯罪。 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 

I chose two Precinct for this demonstration. 我为这个演示选择了两个Precinct You want to create a list with ggplot objects, draw and save maps. 您想要创建一个包含ggplot对象的列表,绘制并保存地图。 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. baptiste的答案将帮助您选择保存图像的最佳方式。 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))

在此输入图像描述 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM