gpt4 book ai didi

r - ggplot - 在 map 顶部创建一个边界叠加层

转载 作者:行者123 更新时间:2023-12-03 13:54:47 25 4
gpt4 key购买 nike

因此,我正在尝试根据自定义变量创建带有边界的佛罗里达县级 map 。我在此处包含了我正在尝试创建的旧版 map

从本质上讲,该 map 显示了佛罗里达州各县的区域分割,媒体市场用粗体黑线边框勾勒。我能够很容易地绘制区域。我希望添加的是在媒体市场变量“MMarket”定义的区域之外的更粗的黑线边框,类似于上面显示的 map 。填充变量将为 Region,媒体市场边界轮廓将使用 MMarket 定义。以下是读取和强化数据的方式:

#read in data
fl_data <- read_csv("Data for Mapping.csv")

#read in shapefiles
flcounties1 <- readOGR(dsn =".",layer = "Florida Counties")

#Fortify based on county name
counties.points <- fortify(flcounties1, region = "NAME")
counties.points$id <- toupper(counties.points$id)

#Merge plotting data and geospatial dataframe
merged <- merge(counties.points, merged_data, by.x="id", by.y="County", all.x=TRUE)
fl_data 对象包含要映射的数据(包括媒体市场变量),并将 shapefile 数据读入 flcounties1 。这是我正在使用的合并数据框的示例:
 head(merged %>% select(id:group, Region, MMarket))
id long lat order hole piece group Region MMarket
1 ALACHUA -82.65855 29.83014 1 FALSE 1 Alachua.1 Panhandle Gainesville
2 ALACHUA -82.65551 29.82969 2 FALSE 1 Alachua.1 Panhandle Gainesville
3 ALACHUA -82.65456 29.82905 3 FALSE 1 Alachua.1 Panhandle Gainesville
4 ALACHUA -82.65367 29.82694 4 FALSE 1 Alachua.1 Panhandle Gainesville
5 ALACHUA -82.65211 29.82563 5 FALSE 1 Alachua.1 Panhandle Gainesville
6 ALACHUA -82.64915 29.82648 6 FALSE 1 Alachua.1 Panhandle Gainesville

我可以使用以下代码很容易地获得区域变量的 map : (here is a picture of the map)
ggplot() +
# county polygons
geom_polygon(data = merged, aes(fill = Region,
x = long,
y = lat,
group = group)) +
# county outline
geom_path(data = merged, aes(x = long, y = lat, group = group),
color = "black", size = 1) +
coord_equal() +
# add the previously defined basic theme
theme_map() +
labs(x = NULL, y = NULL,
title = "Florida: Regions by County") +
scale_fill_brewer(palette = "Set3",
direction = 1,
drop = FALSE,
guide = guide_legend(direction = "vertical",
title.hjust = 0,
title.vjust = 1,
barheight = 30,
label.position = "right",
reverse = T,
label.hjust = 0))

最佳答案

如果您想进入 sf,这里有一个快速示例与 ggplot2::geom_sf .由于我没有您的 shapefile,我只是使用 tigris 下载康涅狄格州的县分割 shapefile ,然后将其转换为简单的 features 对象。

更新说明:随着 sf 的更新版本,一些事情似乎发生了变化。 ,这样你现在应该只用 summarise 将城镇合并为县。 .

# download the shapefile I'll work with
library(dplyr)
library(ggplot2)
library(sf)

ct_sf <- tigris::county_subdivisions(state = "09", cb = T, class = "sf")

如果我想按原样绘制这些城镇,我可以使用 ggplotgeom_sf :
ggplot(ct_sf) +
geom_sf(fill = "gray95", color = "gray50", size = 0.5) +
# these 2 lines just clean up appearance
theme_void() +
coord_sf(ndiscr = F)



分组和调用 summarise没有功能为您提供了几个功能的结合。我将根据县 FIPS 代码(即 COUNTYFP)来联合城镇。柱子。 sf函数适合 dplyr管道,这太棒了。

所以这:
ct_sf %>% 
group_by(COUNTYFP) %>%
summarise()

会给我一个 sf对象所有城镇都已合并到他们的县。我可以将这两者结合起来得到第一个城镇的 map geom_sf层,并在第二层中即时对县进行联合:
ggplot(ct_sf) +
geom_sf(fill = "gray95", color = "gray50", size = 0.5) +
geom_sf(fill = "transparent", color = "gray20", size = 1,
data = . %>% group_by(COUNTYFP) %>% summarise()) +
theme_void() +
coord_sf(ndiscr = F)



没有了 fortify !

关于r - ggplot - 在 map 顶部创建一个边界叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497182/

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