我知道這個問題已經被回答elsewhere已經。我試圖按照@jlhoward的指示,但顯然我的技能太有限了。我可以再問你的幫助,R社區嗎?R ggplot2合併shapefile和csv數據以填充多邊形 - 具體
這是我有:
一種瑞士shape文件:Link
和相應的CSV文件各市的名字,以及他們的拉鍊碼:Link
數據的網站:cadastre.ch
上次投票的其他數據:Direct link, excel-file
我通過合併將一列添加到CSV文件(wow.csv)(我想說明的數據)。該文件現在看起來是這樣的:
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
後來我試圖按照@jlhoward的指令:
- 進口溫度數據文件
- 進口多邊形直轄市shape文件
- 轉換
muni
多邊形繪製數據幀 - 加入從
[email protected]
到ch12.df
的列 - 加入列從
wow
到ch12.df
- 讓劇情
我用下面的代碼試了一下:
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
[email protected]$id <- rownames([email protected])
ch12.df <- fortify(ch12)
ch12.df <- join(ch12.df, [email protected], 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
好,直到最後一步,R工作(到目前爲止我可以告訴)但如果我嘗試創建ggplot,則不會出現錯誤,並且R會以某種方式終止。我想要實現的是根據數據控制多邊形的顏色(在我的城市中)...
任何人都可以幫助我嗎?
[R ggplot2合併shapefile和csv數據以填充多邊形]的可能重複(http://stackoverflow.com/questions/19791210/r-ggplot2-merge-with-shapefile-and-csv-data-to-填充多邊形) – zx8754
是的,這正是我第一次表明的。但不幸的是,我無法用jlhoward的解釋來實現... :-( – Thomas
)如果您可以添加更多關於系統的細節:'sessionInfo()',那麼也會嘗試一點一點地運行'ggplot'。 – zx8754