I have found similar answers to questions like this one, but most of them are using packages rworldmap
, ggmap
, ggsubplot
or geom_subplot2d
. See for example here or here.
I'd like to know how I can plot other ggplot-objects such as a bar-chart onto a map, that is created from a shapefile. The one I'm using can be downloaded here.
EDIT
As @beetroot correctly pointed out, the new file which can be downloaded under the link posted above has changed significantly. Therefore the names of the shapefile etc. are adjusted.
library(rgdal)
library(ggplot2)
library(rgeos)
library(maptools)
map.det<- readOGR(dsn="<path to your directory>/swissBOUNDARIES3D100216/swissBOUNDARIES3D/V200/SHAPEFILE_LV03", layer="VECTOR200_KANTONSGEBIET")
map.kt <- map.det[map.det@data$KANTONSNUM=="CH01000000"|map.det@data$KANTONSNUM=="CH19000000",]
#get centroids
map.test.centroids <- gCentroid(map.kt, byid=T)
map.test.centroids <- as.data.frame(map.test.centroids)
map.test.centroids$KANTONSNR <- row.names(map.test.centroids)
#create df for ggplot
kt_geom <- fortify(map.kt, region="KANTONSNUM")
#Plot map
map.test <- ggplot(NULL)+
geom_polygon(data=kt_geom, aes(long, lat, group=group), fill="white")+
coord_fixed()+
geom_path(data=kt_geom, color="gray48", mapping=aes(long, lat, group=group), size=0.2)+
geom_point(data=map.test.centroids, aes(x=x, y=y), size=9, alpha=6/10)
mapp
This results in such a map. So far so good.
However, I'm having difficulties combining two plots such as the map map.test
and, for example, this one:
geo_data <- data.frame(who=rep(c(1:2), each=2),
value=as.numeric(sample(1:100, 4, replace=T)),
KANTONSNR=rep(c(1,19), 2))
bar.testplot <- ggplot()+
geom_bar(data=geo_data, aes(factor(id),value,group=who),position='dodge',stat='identity')
The barcharts should lie at the center of the two polygons, i.e. where the two points are. I could produce the barcharts and plot them onto the map separately, if that makes things easier.