2017-06-13 47 views
-1

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

我正在寻找一种方法来结合它们,覆盖它们,所以最后我有一个沿海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([email protected])<-pies1$pie 
pi1<-tidy(pies1) 

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

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

+0

我没有碰到过一个简单的解决办法这样做。我之前完成这个工作的方式是首先使用plotGoogleMaps包来绘制饼图,将其转换为空间多边形,然后与ggmap进行集成。可以试着向你展示一个例子(但需要花费很多时间来解释)。你也可以参考[this](https://gis.stackexchange.com/questions/214810/pie-charts-on-gis-maps-using-r) –

+2

欢迎来到Stackoverflow!请花些时间阅读[帮助页面](http://stackoverflow.com/help),尤其是名为[“我可以问些什么话题?”]的章节(http://stackoverflow.com/help/)讨论话题)和[“我应该避免问什么类型的问题?”](http://stackoverflow.com/help/dont-ask)。请参阅[tour](http://stackoverflow.com/tour)并阅读[如何提出良好问题](http://stackoverflow.com/help/how-to-ask)。最后,请学习如何创建[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – Markus

+0

您希望用饼图表示什么类型的渔业数据?抓住每工作单位?物种组成? – ccapizzano

回答

0

这是一种将饼图作为空间多边形的方法。希望你可以用你的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([email protected])<-pies$pie 
pi<-tidy(pies) 

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

enter image description here

+0

非常感谢!这似乎是它会做的伎俩,我明天会尝试一下,看看它是怎么回事:) –

相关问题