gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:38:31 25 4
gpt4 key购买 nike

我对使用 R 比较陌生,我正在尝试使用数据绘制某些区域的轮廓和颜色来创建美国各州的 map 。我正在尝试显示一个州及其黑色轮廓的县。最重要的是,我想在县组周围创建粗红色边框,并根据我拥有的一些数据对一些县进行颜色填充。

本质上我想结合这两个图像:

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

最佳答案

根据 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

关于r - 如何使用 ggplot2 在一组美国县周围创建边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com