如何将州名称转换为纬度和经度以在 R 中使用 ggvis 创建等值线?
How to convert state name to latitude and longitude to create choropleth with ggvis in R?
我想用 ggvis 包为每个州的收入值创建一个等值线。下面是我的数据。我认为映射数据需要纬度和经度列。任何人都知道如何将州名称转换为纬度和经度?非常感谢!
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))
这是一种方法。我使用的是 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()
所以这里有一个 ggvis
解决方案(可能应该是一个单独的答案,但 WTH)。
事实证明,上面的 shapefile 分辨率非常高,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)
有一篇关于使用 ggvis
here 创建等值线图的不错的博客 post。如果您想要自定义颜色(如之前的回答),请参阅 post。
我想用 ggvis 包为每个州的收入值创建一个等值线。下面是我的数据。我认为映射数据需要纬度和经度列。任何人都知道如何将州名称转换为纬度和经度?非常感谢!
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))
这是一种方法。我使用的是 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()
所以这里有一个 ggvis
解决方案(可能应该是一个单独的答案,但 WTH)。
事实证明,上面的 shapefile 分辨率非常高,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)
有一篇关于使用 ggvis
here 创建等值线图的不错的博客 post。如果您想要自定义颜色(如之前的回答),请参阅 post。