我目前正在绘制渔业数据,并设法在ggplot中沿着海岸形状文件分别绘制海洋中不同省份的多边形shapefile。此外,我已经制作了馅饼图,在海洋的情节中,我添加了馅饼与add.pie(mapplots包)。
我正在寻找一种方法将两者结合起来,覆盖它们,所以最后我有一个沿海形状文件,省份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没有显示任何内容的地方。如果您需要更多关于我可以更新的信息。
答案 0 :(得分: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(pies@polygons)<-pies$pie
pi<-tidy(pies)
ggplot() +
geom_polygon(data=pi, aes(x=long, y=lat, group=id, fill=.id))