【问题标题】:Is there a way to plot pies, and polygons from shapefiles on the same plot in R?有没有办法在 R 中的同一图上绘制饼图和 shapefile 中的多边形?
【发布时间】:2017-06-13 08:36:41
【问题描述】:

我目前正在绘制渔业数据,并设法在 ggplot 的沿海 shapefile 上分别绘制海洋中不同省份的多边形 shapefile。另外,我制作了饼图,在海洋图上,我使用 add.pie(mapplots 包)添加了饼图。

我正在寻找一种将它们结合起来、覆盖它们的方法,所以最后我有一个沿海 shapefile、省份 shapefile 和顶部的馅饼。我怎么能这样做,有人有什么想法吗?

非常感谢!

更新:我尝试使用 plotGoogleMaps 包绘制饼图,以便将 t 导出为 shapefile(这将是一个理想的解决方案),但由于某种原因,当我最终尝试绘制它们时,没有显示饼图。 ..我附上代码,也许你们中更有经验的人会知道我做错了什么?再次感谢:)

library(sp)
library(plotGoogleMaps)

data<-read.csv("cdis5014_all9sp.csv")
# transform the data then change into large spdf
names(data)[1]<-c("Species")

TotalCatch15 <- aggregate(data$Catch_t, list(data$Species,data$YearC, data$xLon5ctoid, data$yLat5ctoid), sum) # per species, per gear, per year, per cell
names(TotalCatch15)<-c("Species", "Year", "Long", "Lat", "tCatch")

# now subset only years 2000-2014
?subset
last15yrs <- subset(TotalCatch15, Year %in% 2000:2014) 

# now average it
AvgCatch15 <- aggregate(last15yrs$tCatch, list(last15yrs$Species, last15yrs$Long, last15yrs$Lat), mean) # per species, per cell!
names(AvgCatch15)<-c("Species", "Long", "Lat", "tCatch")

AvgCatch15$Species

# now try to transform it to make these pies?
# if needed   AvgCatch15$Species <- as.character (AvgCatch15$Species)
?spread
pieready <- spread(AvgCatch15, Species, tCatch, fill=0)
summary(pieready)

coordinates(pieready)<-~Long+Lat
proj4string(pieready) <- CRS('+init=epsg:4326')   #epsg can also be 32662?
piereadyshp <- spTransform(pieready, CRS("+proj=longlat +datum=WGS84"))
summary(piereadyshp)
?spTransform

#using plotGoogleMaps::pieSP to generate the spatial data.frame for pie-chart
?pieSP
pies1 <- pieSP(pieready, zcol= c("ALB", "BET", "BFT", "BUM", "SAI", "SKJ", "SWO", "WHM", "YFT"), max.radius=500)
pies1$pie=rep(c("ALB", "BET", "BFT", "BUM", "SAI", "SKJ", "SWO", "WHM", "YFT"),345)

# Extract spatial polygon data.frame 

library(broom)
library(ggplot2)

names(pies1@polygons)<-pies1$pie
pi1<-tidy(pies1)

ggplot() +
  geom_polygon(data=pi1, aes(x=long, y=lat, group=id, fill=.id))

这是 ggplot 不显示任何内容的地方。如果您需要有关任何内容的更多信息,我可以更新它。

【问题讨论】:

  • 我没有找到一个简单的解决方案。我之前这样做的方法是首先使用 plotGoogleMaps 包来绘制饼图,将其转换为空间多边形,然后与 ggmap 集成。可以尝试向您展示一个示例(但这需要很多时间来解释)。也可以参考this
  • 欢迎来到 Stackoverflow!请花点时间阅读the help pages,尤其是名为"What topics can I ask about here?""What types of questions should I avoid asking?" 的部分。也请使用tour 并阅读有关how to ask good questions 的信息。最后请学习如何创建Minimal, Complete, and Verifiable Example
  • 您希望用饼图表示哪种类型的渔业数据?投入产出比?物种组成?
  • 如果问题不符合指导方针,我很抱歉,我希望我说得足够清楚。我代表的是 5x5 单元格的捕获数据,并且饼图针对不同的物种进行分区,并且大小根据捕获值而有所不同。

标签: r plot ggplot2 pie-chart


【解决方案1】:

这是一种将饼图作为空间多边形的方法。希望您可以将它与您的 shp 文件与 ggmap 集成:

library(sp)
library(plotGoogleMaps)
data(meuse)
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')
df <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84"))

#using plotGoogleMaps::pieSP to generate the spatial data.frame for pie-chart
pies <- pieSP(df,zcol=c('zinc','lead','copper'), max.radius=50)
pies$pie=rep(c('zinc','lead','copper'),155)

# m=plotGoogleMaps(pies, zcol='pie') #run this to show the java-based output of piechart on map

#Extract spatial polygon data.frame 

library(broom)
library(ggplot2)

names(pies@polygons)<-pies$pie
pi<-tidy(pies)

ggplot() +
   geom_polygon(data=pi, aes(x=long, y=lat, group=id, fill=.id))

【讨论】:

  • 非常感谢!这似乎可以解决问题,我明天试试看效果如何:)
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2018-01-17
相关资源
最近更新 更多