shapefile以在R中生成链接的微图 [英] shapefile to produce a linked micromap in R
问题描述
我试图用美国50个州的贫困率复制链接地图的micromap R包中提供的示例.我正在尝试使用德国的Shapefile获得相同的结果.这是我的代码:
I am trying to replicate the example provided in the micromap R package of a linked map with the poverty rate for the 50 US states. I am trying to achieve the same results with a Shapefile from Germany. Here is my code:
library(micromap)
library(ggplot2)
setwd ('C:/Users/Jesus/Dropbox/linked maps/Chapter4')
ger<-readShapePoly("germany3.shp")
edPov<-read.csv('gerpoverty.csv')
statePolys <- create_map_table(ger, 'VARNAME_1')
head(statePolys)
lmplot(stat.data=edPov,
map.data=statePolys,
panel.types=c('labels', 'dot','map'),
panel.data=list('Id1','poverty',NA),
ord.by='poverty',
grouping=5, median.row=F,
map.link=c('Id1','VARNAME_1'))
当我尝试将其解析为用于ggplot2的平面表时,该过程不起作用(而不是具有16个观测值,而是具有8000多个观测值).另外,当我尝试绘制链接的地图时,屏幕上会出现以下错误:
When I tried to parse it into a flat table for use with ggplot2 the procedure does not work (instead of having 16 observations it has 8000 plus observations). Also when I try to plot the linked map, the following error appears on screen:
Error in `[.data.frame`(DF, , ord.by) : undefined columns selected
以下是zip文件中文件的链接:
Here is the link to the files in a zip file:
https://www.dropbox.com/s/c43k755aadvu2z6/germany.rar
有什么想法或建议吗?
谢谢
推荐答案
问题是create_map_table(...)
将您提供的所有内容作为id列(在您的情况下为VARNAME_1
),并将其放在名为<输出中的c2>(在您的情况下为statePolys
).因此,现在有了一个statePolys$ID
列,其中包含您在VARNAME_1
中所拥有的内容.当您告诉lmplot(...)
加入edPov$Id1
和statePolys$VARNAME_1
时,它找不到后一列.换句话说,map.link
的第二个元素必须始终为ID
.这样的代码行得通(我在edPov
和ger
中都使用了ID_1列,因为它似乎包含一个德国州的ID).
The problem is that create_map_table(...)
takes whatever you give it as the id column (VARNAME_1
in your case), and puts that in a column called ID
in the output (statePolys
in your case). So now there is a statePolys$ID
column with whatever you had in VARNAME_1
. When you tell lmplot(...)
to join on edPov$Id1
and statePolys$VARNAME_1
, it can't find the latter column. Another way to say this is that the second element of map.link
must always be ID
. So this code works (I used the ID_1 column in both edPov
and ger
here, since that seems to contain a German State ID).
library(micromap)
library(ggplot2)
ger<-readShapePoly("germany3.shp")
edPov<-read.csv('gerpoverty.csv')
statePolys <- create_map_table(ger, 'ID_1') # ID_1 stored in statePolys$ID
head(statePolys)
lmplot(stat.data=edPov,
map.data=statePolys,
panel.types=c('labels', 'dot','map'),
panel.data=list('Id1','poverty',NA),
ord.by='poverty',
grouping=5, median.row=F,
map.link=c('ID_1','ID'))
在这里,ID_1
指的是edPoly$ID_1
,而ID
指的是statePolys$ID
.
Here, ID_1
refers to edPoly$ID_1
, and ID
refers to statePolys$ID
.
这篇关于shapefile以在R中生成链接的微图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!