根据NPA(地区)R地图瑞士

S12*_*000 8 maps r ggplot2

我打算在瑞士做一个调查.NPA将被问到.

NPA(邮政编码)包含4个号码.

  • 例如,1227年是卡鲁日的NPA(日内瓦州 - 瑞士的一部分).
  • 例如,1784年是Courtepin的NPA(弗里堡州 - 瑞士州的一部分).
  • 等等

我想知道如何在地图上表示所有观察(大约1500).我正在考虑使用ggplot,因为我将它用于其他图形(我认为ggplot是"漂亮的").但是,我对任何其他建议持开放态度.

以下是一些虚假数据:http: //pastebin.com/HsuQnLP3

瑞士地图的输出应该有点像美国地图(图片来源:http://www.openintro.org)

在此输入图像描述

更新:

我试过创建一些代码:

library(sp)
test <-  url("https://dl.dropboxusercontent.com/u/6421260/CHE_adm3.RData")
print(load(test))
close(test)

gadm$NAME_3
gadm$TYPE_3
Run Code Online (Sandbox Code Playgroud)

但似乎http://gadm.org/没有提供公社的NPA ......

新更新:

我发现(感谢@yrochat)NPA的shapefile:http: //www.cadastre.ch/internet/cadastre/fr/home/products/plz/data.html

我称为ZIP文件:Shape LV03

然后我试过了

library("maptools")
swissmap <- readShapeLines("C:/Users/yourName/YourPath/PLZO_SHP_LV03/PLZO_PLZ.shp")
plot(swissmap)
data <- data.frame(swissmap)
data$PLZ #the row who gives the NPA
Run Code Online (Sandbox Code Playgroud)

由于我在shapefile上有PLZ,我如何在地图上为我的观察着色?我提供了一些关于数据的假数据http://pastebin.com/HsuQnLP3

在此输入图像描述

谢谢

Slo*_*ner 8

好的,使用shapefile,我们可以很容易地绘制事物.

work.dir <- "directory_name_no_trailing slash"

# open the shapefile
require(rgdal)
require(rgeos)
require(ggplot2)
ch <- readOGR(work.dir, layer = "PLZO_PLZ")

# convert to data frame for plotting with ggplot - takes a while
ch.df <- fortify(ch)

# generate fake data and add to data frame
ch.df$count <- round(runif(nrow(ch.df), 0, 100), 0)

# plot with ggplot
ggplot(ch.df, aes(x = long, y = lat, group = group, fill = count)) +
    geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
    theme()

# or you could use base R plot
ch@data$count <- round(runif(nrow(ch@data), 0, 100), 0)
plot(ch, col = ch@data$count)
Run Code Online (Sandbox Code Playgroud)

就个人而言,我觉得ggplot使用起来要容易得多plot,而且默认输出效果要好得多.

截图

ggplot使用简单的数据框,这使得它易于子集化.

# plot just a subset of NPAs using ggplot
my.sub <- ch.df[ch.df$id %in% c(4,6), ]
ggplot(my.sub, aes(x = long, y = lat, group = group, fill = count)) +
    geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
    theme()
Run Code Online (Sandbox Code Playgroud)

结果:

screenshot2