2017-06-13 55 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

非常感謝!這似乎是它會做的伎倆,我明天會嘗試一下,看看它是怎麼回事:) –

相關問題