根据NPA(地方)R地图瑞士 [英] R map switzerland according to NPA (locality)

查看:191
本文介绍了根据NPA(地方)R地图瑞士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在瑞士做一个调查。 NPA将被问及。



不提供公社的NPA .. 。



新更新

我找到了(谢谢@yro聊天)带有NPA的shapefile:
http: //www.cadastre.ch/internet/cadastre/fr/home/products/plz/data.html



我的ZIP文件叫做:形状LV03



然后我试过了

  library(maptools )
swissmap< - readShapeLines(C:/Users/yourName/YourPath/PLZO_SHP_LV03/PLZO_PLZ.shp)
plot(swissmap)
data< - data.frame(swissmap )
data $ PLZ#给出NPA的行

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





谢谢

解决方案

好吧,使用shapefile,我们可以很容易地绘制事情。
$ b

  work.dir<  - directory_name_no_trailing slash

#打开shapefile
require(rgdal)
require(rgeos)
require(ggplot2)
ch < - readOGR(work.dir,layer =PLZO_PLZ)

#转换为数据框进行绘图ggplot - 需要一段时间
ch.df< - fortify(ch)

#生成假数据并添加到数据框
ch.df $ count < - round( runif(nrow(ch.df),0,100),0)

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

#或者你可以使用base R图
ch @ data $ count < - round(runif(nrow(ch @ data),0,100),0)
plot(ch,col = ch @ data $ count)

我个人发现 ggplot plot 工作起来容易得多,而且默认输出看起来更好。





和<$ c $

 #绘图只是一个简单的数据框架,它可以很容易地被子集化。使用ggplot 
my.sub< ch.df [ch.df $ id%in%c(4,6),]
ggplot(my.sub,aes(x = ($)
$ geom_polygon(color =black,size = 0.3,aes(group = group))+
theme()

结果:


I plan to do a survey in Switzerland. NPA will be asked.

NPA (postal codes) contains 4 number.

  • For instance 1227 is the NPA of Carouge (part of canton Geneva - Switzerland).
  • For instance 1784 is the NPA of Courtepin (part of canton Fribourg -Switzerland).
  • etc.

I would like to know how to represent all observation (about 1500) on a map. I was thinking using ggplot as I use it for other graphs (I think ggplot is "beautiful"). However, I'm open to any other suggestion.

Here are some fake data: http://pastebin.com/HsuQnLP3

The output for the swiss map should be a bit like that USA map (credit:http://www.openintro.org)

Update:

I've tried to create some code :

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

gadm$NAME_3
gadm$TYPE_3

But it seems http://gadm.org/ doesn't provide the NPA of the communes...

New update:

I've find (thanks @yrochat) a shapefile with NPA: http://www.cadastre.ch/internet/cadastre/fr/home/products/plz/data.html

I'ts the ZIP file called : Shape LV03

Then I've tried

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

As I have the PLZ on a shapefile, how do i color my observation on the map? I provided some fake data on data http://pastebin.com/HsuQnLP3

Thanks

解决方案

OK, with the shapefile, we can plot things easily enough.

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)

Personally I find ggplot a lot easier to work with than plot and the default output is much better looking.

And ggplot uses a straightforward data frame, which makes it easy to subset.

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

Result:

这篇关于根据NPA(地方)R地图瑞士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆