Cyr*_*ian 6 mapping r ggplot2 ggmap
我想创建一张显示州和县边界(即不同颜色的州边界)的美国地图。我通常使用导入的形状文件或使用 的函数来执行此ggplot2操作map_data。然而,我面临三个障碍。
1)我无法在我的计算环境中安装gdal和geos,因此无法使用任何形状文件或 GeoJSON 文件(我尝试加载使用的地图县级形状文件fastshp尚未成功,但我愿意接受任何可以重现地图的解决方案低于但包括州边界)。
map_data2)我需要包括夏威夷和阿拉斯加,这样就排除了from的使用ggplot2。
3)我需要地图包括州和县边界,这使得使用usmap包成为问题,因为它是包装函数,ggplot2但没有轻松和一般的能力来定制原始ggplot2对象的级别。
4)此外,无法使用sf包 bc 它具有非 R 库依赖项(units包依赖于 C 库libudunits2)。
我需要什么:一张可以投影阿拉斯加和夏威夷并使用对比色显示州和县边界的地图,我需要完成所有这一切,而无需诉诸任何依赖rgeos、rgdal和/或 的软件包units。
到目前为止我plot_usmap从usmap包中尝试过的内容:
library(dplyr)
library(stringr)
library(ggplot2)
library(usmap)
library(mapproj)
devtools::install_github("wmurphyrd/fiftystater")
library(fiftystater)
county_data<-read.csv("https://www.ers.usda.gov/webdocs/DataFiles/48747/PovertyEstimates.csv?v=2529") %>% #
filter(Area_name != "United States") %>%
select(FIPStxt, Stabr, Area_name, PCTPOVALL_2017) %>%
rename(fips = FIPStxt)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
state_map <- map_data("state")
plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") +
geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") +
geom_path(data = state_map, aes(x =long , y=lat), color= "red")+
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
theme(legend.position = "none") +
theme_map() #no go
plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") +
geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
theme(legend.position = "none") +
theme_map() #no go
plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") +
geom_map(data = crimes, aes(map_id = state, color= "red"), map = fifty_states) +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
theme(legend.position = "none") +
theme_map() #no go
Run Code Online (Sandbox Code Playgroud)
我怀疑正在发生的事情是,一层(原始ggplot代码)使用与另一层不同的 CRS 系统进行投影 - 由plot_usmap. 第二层会产生一个非常小的红点(参见下图中的圆圈)。不确定如何在未安装 geos/gdal 的情况下重新投影。请参阅下面的地图,其中黑色圆圈突出显示红点所在的位置。
好吧,经过包作者的一些建议和我自己的一些修改,我终于能够得到我想要的输出。
对于希望生成包含阿拉斯加和夏威夷在内的美国地图的人们来说,这种方法是理想的选择...
1) 无法在 R 引擎运行的环境中安装非 R 软件包(例如缺乏管理员访问权限)
2) 需要使用对比色绘制县和州边界地图
library(dplyr)
library(ggplot2)
library(usmap)
#Example data (poverty rates)
county_data<-read.csv("https://www.ers.usda.gov/webdocs/DataFiles/48747/PovertyEstimates.csv?v=2529") %>% #
filter(Area_name != "United States") %>%
select(FIPStxt, Stabr, Area_name, PCTPOVALL_2018) %>%
rename(fips = FIPStxt)
states <- plot_usmap("states",
color = "red",
fill = alpha(0.01)) #this parameter is necessary to get counties to show on top of states
counties <- plot_usmap(data = county_data,
values = "PCTPOVALL_2018",
color = "black",
size = 0.1)
Run Code Online (Sandbox Code Playgroud)
us_mapggplot() +
counties$layers[[1]] + #counties needs to be on top of states for this to work
states$layers[[1]] +
counties$theme +
coord_equal() +
theme(legend.position="none") +
scale_fill_gradient(low='white', high='grey20') #toggle fill schema using vanilla ggplot scale_fill function
Run Code Online (Sandbox Code Playgroud)
us_map仅使用从包中获得的原始数据ggplot() +
geom_polygon(data=counties[[1]],
aes(x=x,
y=y,
group=group,
fill = counties[[1]]$PCTPOVALL_2018),
color = "black",
size = 0.1) +
geom_polygon(data=states[[1]],
aes(x=x,
y=y,
group=group),
color = "red",
fill = alpha(0.01)) +
coord_equal() +
theme_map() +
theme(legend.position="none") +
scale_fill_gradient(low='white', high='grey20')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3263 次 |
| 最近记录: |