如何使用ggplot2在一组美国县周围创建边框?

3
我相对使用R较新,尝试使用数据为美国各州创建地图以勾勒和着色特定区域。我尝试展示一个州及其县的黑色轮廓。在此基础上,我想创建厚重的红色边框以围绕一些县,并根据我拥有的一些数据填充某些县的颜色。

基本上,我想要将这两个图像结合起来:

I would like to changed this map to outlines of the coloured areas, so -for example - there would be a red border around everything blue.

Then I would like to fill the map above like this

这是我迄今为止尝试完成此任务所编写的代码:

# Maping IA, plan 74406IA0010001

# Importing data 
library(ggplot2) 
library(ggmap) 
library(maps) 
library(mapdata) 
library(stringr) 
library(plyr) 
library(dplyr)

setwd("/Users/erinmay/Desktop/WL_RA/marketplace2/data")

county <- map_data("county") 
plan <- read.csv("IA_2017.csv")

# Using subset

iowa <- subset(county, region=="iowa") #county point files for iowa

# Merging in map data

countyplan <- merge(x=iowa, y=plan, by=c("region","subregion"), all.x=TRUE)

countyplan <- countyplan[order(countyplan$chosen_plan),]

# Creating map  

final <- ggplot(data=countyplan) + 
           geom_path(aes(x=long,y=lat,group=RatingArea),colour='black') +  
           geom_polygon(aes(x=long,y=lat,group=group,fill=chosen_plan)) + 
           coord_map() + coord_fixed(1.3)

ggsave(final,height=6,width=10,unit='in',file='iowa.pdf')

感谢您的帮助!以下是数据: https://www.dropbox.com/s/x8x2l50dvmg0lsb/QHP_IA_2017.csv?dl=0

我们能否有一些样本数据?这将有助于测试解决方案。请参见mcveR中可重现的示例 - Z.Lin
示例数据的链接已经失效了,有没有人有这个数据或者至少有样本呢? - Trenton Hoffman
1个回答

1

根据OP的澄清,编辑了答案,仅对每个评分区域的外部边框进行着色:

就我所理解的而言,在ggplot中,所有多边形都是平等创建的。因此,如果它必须着色多边形的轮廓,则会对所有边进行着色,而不管两个相邻的多边形是否属于同一评级区域。

您需要在将多边形转换为数据框之前将其溶解在同一个规划区域中。(注意:您可以将现有的数据框转换回多边形,但从原始数据源获取多边形数据可能更容易。)

library(maps); library(dplyr); library(tidyr); library(maptools); library(rgeos)

# get map data
county_map <- map("county", fill = T, plot = FALSE)

# create mapping table between county names & rating areas
county_map_match <- data.frame(name = county_map$names) %>%
  separate(name, c("region", "subregion"), sep = ",", remove = FALSE) %>%
  left_join(plan %>% select(region, subregion, RatingArea))
rownames(county_map_match) <- county_map_match$name

# convert map to SpatialPolygon, then join with mapping table for SpatialPolygonDataFrame
county_map <- map2SpatialPolygons(county_map, IDs = county_map$names)
county_map <- SpatialPolygonsDataFrame(county_map, county_map_match)

# remove invalidities in the county map
gIsValid(county_map) #returns FALSE: there are invalid self-intersecting geometries in the polygons, which will cause problems
county_map <- gBuffer(county_map, byid = TRUE, width = 0)
gIsValid(county_map) #returns TRUE

# dissolve county map by rating area & fortify to data frame
area_map <- unionSpatialPolygons(county_map, IDs = county_map$RatingArea)
area_map <- fortify(area_map)
area_map$group <- gsub(".1", "", x= area_map$group, fixed = T)

一旦您获得了评级区域的数据框版本,您可以将其纳入ggplot中:
ggplot(countyplan,
       aes(x=long,y=lat, group = group, fill = chosen_plan)) + 
  geom_polygon(size = 0.5, colour = "black") +
  geom_polygon(data = area_map, 
            aes(x=long, y=lat, group = group, colour = group), 
            fill = NA, size = 2) +
  scale_fill_manual(name = "Chosen Plan", values = c("darksalmon"), na.value = "grey") +
  scale_color_discrete(name = "Rating Area") +
  coord_map() + coord_fixed(1.3)

edited ggplot with outer borders coloured

你可以从RColorBrewer包中获取更好的颜色调色板,并在scale_XX_brewer()调用中使用它们,如果你喜欢的话。单个颜色的名称可以在这里找到:http://sape.inf.usi.ch/quick-reference/ggplot2/colour

差不多了!这非常有帮助,非常感谢。有没有办法将评分区域的内部线条更改为黑色,只保留评分区域的外边框着色? - Erin
@Erin 修改了我的解决方案。 - Z.Lin
这对我有用。对于任何试图将已溶解的多边形映射到使用UTM坐标和AEA投影的usmap()的人,您需要在绘图之前运行以下命令: area_map <- usmap_transform(data=area_map, input_names=c("long", "lat")) 然后使用以下命令绘制数据: plot_usmap() + geom_polygon(data=area_map, aes(x=x, y=y, group=group, color=group), etc. - BonnieM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接