'Is there a way to plot pies, and polygons from shapefiles on the same plot in R?

I am currently plotting fisheries data and have managed to plot separately polygons shapefile of different provinces in the ocean over the coastal shapefile in ggplot. Also, I've made pie plots, where over a plot of the ocean I have added pies with add.pie (mapplots package).

I am looking for a way to combine them both, overlay them, so in the end I have a coastal shapefile, provinces shapefile and pies on top. How could I do this, does anyone have any ideas?

Thank you very much!

Update: I tried plotting the pies with plotGoogleMaps package in order to export t as a shapefile (which would be an ideal solution), but for some reason when I try to plot them in the end, there are no pies showing... I'm attaching the code, maybe the more experienced of you will know what I did wrong? Thanks again :)

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))

This is where ggplot doesn't show anything. If you need more info about anything I can update it.



Solution 1:[1]

Here's a way to get pie chart as spatial polygons. Hopefully you could integrate this along with your shp files with 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))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam Quek