简体   繁体   English

如何在R中使用ggvis将州名转换为纬度和经度以创建Choropleth?

[英]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.

相关问题 将经纬度坐标转换为R中的国名 - Convert latitude and longitude coordinates to country name in R 如何从R中的州边界获取经纬度数据 - How to get latitude and longitude data from the borders of a state in R 如何在纬度和经度中从 R 中的 PostGis 转换 Geom 对象? - How to convert Geom object from PostGis in R in latitude and longitude? R - 如何将纬度和经度坐标转换为地址/人类可读位置? - R - How to convert latitude and longitude coordinates into an address/ human readable location? 如何在R中将英国网格参考转换为经度和纬度 - How to convert UK grid reference to latitude and longitude in R 如何从R中的城市名称和国家/地区获取经度和纬度坐标? - How to get the longitude and latitude coordinates from a city name and country in R? 如何从R中的经度和纬度中获取位置名称,以获取完整的数据集? - How to get location name from latitude and longitude in R for a complete dataset? 将纬度/经度转换为州平面坐标 - Convert latitude/longitude to state plane coordinates 将状态平面坐标(阿拉斯加4)转换为纬度和经度 - Convert state plane coordinates (Alaska 4) to latitude and longitude 纬度经度坐标到 R 中的状态代码 - Latitude Longitude Coordinates to State Code in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM