[英]How to convert state name to latitude and longitude to create choropleth with ggvis in R?
I want to create a choropleth for income values in each state with package ggvis. 我想使用ggvis软件包为每个州的收入值创建一个Choropleth。 Below is my data. 以下是我的数据。 I think latitude and longitude columns are required for mapping the data. 我认为映射数据需要纬度和经度列。 Anyone knows that how can I convert the state name to latitude and longitude? 有人知道如何将州名称转换为纬度和经度吗? Thanks a lot! 非常感谢!
mapdata<-data.frame(
state=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
income=runif(50,min=100,max=9000))
Here's one way. 这是一种方法。 I'm using ggplot
rather than ggVis
but this should get you started. 我使用的是ggplot
而不是ggVis
但这应该可以帮助您入门。
library(raster) # for getData(...)
library(ggplot2)
library(data.table)
usa <- getData('GADM',country='USA',level=1) # shapefile of US states
shp <- usa[(!usa$NAME_1 %in% c("Alaska","Hawaii")),] # remove AK, HI for this example
gg.dt <- setDT(fortify(shp)) # convert shapefile to format ggplot can use
# merge with attribute table
gg.dt <- gg.dt[setDT(cbind(id=rownames(shp@data),shp@data)),on="id"]
gg.dt[,state:=tolower(NAME_1)] # convert state names to lower case
gg.dt <- gg.dt[setDT(mapdata),on="state"] # merge with mapdata
ggplot(gg.dt, aes(x=long, y=lat, group=group,fill=revenue)) +
geom_polygon(color="grey50", size=0.1)+
scale_fill_gradientn(colours=rev(heat.colors(10)))+
coord_map()
So here is a ggvis
solution (probably should be a separate answer, but WTH). 因此,这里有一个ggvis
解决方案(可能应该是一个单独的答案,但是是WTH)。 It turns out that the shapefile above is very high resolution and ggvis
just can't deal with that large a file. 事实证明,上面的shapefile具有很高的分辨率,而ggvis
不能处理那么大的文件。 So here we download a low-res shapefile of US state boundaries, merge using data.frames (which is adequately fast with the low-res shapefile), and then render using ggvis
. 因此,这里我们下载了一个具有美国状态边界的低分辨率shapefile,使用data.frames进行合并(与低分辨率shapefile足够快),然后使用ggvis
进行渲染。
library(rgdal) # for readOGR(...)
library(ggplot2) # for fortify(...)
library(ggvis)
# load low resolution US state shapefile (1:20MM)
url <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip"
tf <- tempfile()
td <- tempdir()
download.file(url,tf, mode="wb") # download shapefile archive of US state boundaries
unzip(tf, exdir=td) # unzip into directory td
usa <- readOGR(dsn=td, layer="cb_2014_us_state_20m")
shp <- usa[(!usa$STUSPS %in% c("AK","HI")),] # remove AK, HI for this example
gg.df <- fortify(shp) # convert shapefile to format ggvis can use
gg.df <- merge(gg.df,cbind(id=rownames(shp@data),shp@data),by="id") # merge with attribute table
gg.df$state <- tolower(gg.df$NAME) # convert state names to lower case
gg.df <- merge(gg.df,mapdata,by="state") # merge with mapdata
gg.df <- gg.df[order(gg.df$order),] # reset to original order
gg.df %>%
group_by(group) %>%
ggvis(~long, ~lat) %>%
layer_paths(fill= ~revenue)
There's a decent blog post on creating choropleth maps using ggvis
here . 有一个关于创建等值线图使用一个体面的博客文章ggvis
这里 。 If you want custom colors (as in the earlier answer), see the post. 如果您想要自定义颜色(如先前的答案所示),请参阅文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.