【问题标题】:How do I use ggplot2 to create a border around a group of US counties?如何使用 ggplot2 在一组美国县周围创建边界?
【发布时间】:2017-08-29 00:07:36
【问题描述】:

我对使用 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

【问题讨论】:

  • 我们可以提供一些示例数据吗?这将有助于测试解决方案。见mcve & reproducible example in R
  • 示例数据的链接已损坏。至少有人有数据或样本吗?

标签: r ggplot2


【解决方案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)

如果您愿意,您可以从 RColorBrewer 包中获得更好的调色板并在 scale_XX_brewer() 调用中使用它们。各个颜色的名称可以参考这里:http://sape.inf.usi.ch/quick-reference/ggplot2/colour

【讨论】:

  • 几乎!这很有帮助,所以非常感谢。有没有办法将评级区域的内部线条更改为黑色,而只留下评级区域的外边界着色?
  • @Erin 编辑了我的解决方案。
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2015-08-12
相关资源
最近更新 更多