I know that this question has been answered elsewhere already. I tried to follow the instructions of @jlhoward but obviously my skills are too limited. May I ask again of your help, R community?
Here's what I have:
A shapefile for Switzerland: Link
And the corresponding CSV-file for the names of municipalities as well as their zip-codes: Link
Website of data: cadastre.ch
Additional data on the last popular vote: Direct link, excel-file
I add a column to the CSV-file (wow.csv) (the data I want to illustrate) by merging. The file looks now like this:
Gemeinden code Ja.Anteil Ortschaft PLZ Zusatzziffer Kantonskürzel E N
1 Aadorf 4551 78.78638 Aawangen 8522 2 TG 710206 263564
2 Aadorf 4551 78.78638 Ettenhausen TG 8356 0 TG 710129 259411
3 Aadorf 4551 78.78638 Aadorf 8355 0 TG 710588 261648
4 Aadorf 4551 78.78638 Guntershausen 8357 0 TG 711741 258934
5 Aadorf 4551 78.78638 Wittenwil 9547 0 TG 712002 262572
Afterwards I tried to follow the instructions of @jlhoward:
- Import temperature data file
- Import polygon shapefile of municipalities
- Convert
muni
polygons to a data frame for plotting - Join columns from
ch12@data
toch12.df
- Join columns from
wow
toch12.df
- Make the plot
I tried it with the following code:
require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")
# read share data and the file from cadastre.ch (zip-codes)
asyl <- <- read.csv("~/FS14-1/PLZO_SHP_LV03/Asylgesetz_csv.csv", sep=";")
mydata1 <- read.table("~/FS14-1/PLZO_SHP_LV03/PLZO_CSV_LV03.csv", sep=";", quote="\"")
#merge the two files
wow <- merge(x = asyl, y = mydata1, by = "Gemeinden", all = TRUE)
# read municipality polygons
ch12 <- readOGR(work.dir, layer = "PLZO_PLZ")
# fortify and merge: muni.df is used in ggplot
ch12@data$id <- rownames(ch12@data)
ch12.df <- fortify(ch12)
ch12.df <- join(ch12.df, ch12@data, by="id")
ch12.df <- merge(ch12.df, wow, by="PLZ", all.x=T, a..ly=F)
#create the map layers
gp <- ggplot(data=ch12.df, aes(x=long, y=lat, group=group)) +
geom_polygon(aes(group = group))+ # draw polygons
geom_path(color="grey", linestyle=2)+ # draw boundaries
coord_equal() +
scale_fill_gradient(low = "#ffffcc", high = "#ff4444",
space = "Lab", na.value = "grey50",
guide = "colourbar")+
labs(title="Zustimmung auf Gemeindelevel")
gp
Well, until the last step, R worked (so far I can tell) but if I try to create the ggplot, I get no an error and R somehow terminates. What I'm trying to achieve is to control the colours (in my case of municipalities) of the polygons depending on data...
Can anyone help me?