ggplot-在地图顶部创建边框叠加层 [英] ggplot - Create a border overlay on top of map
问题描述
因此,我试图基于自定义变量创建带有边框的佛罗里达州县级地图.我在此处尝试创建的地图包含较旧的版本
So I am trying to create a Florida county-level map with borders based on a custom variable. I included an older version of the map that I am trying to create here
从本质上讲,该地图显示了佛罗里达州的各个地区的细分情况,媒体市场的轮廓用黑体黑色边框标出.我能够很容易地绘制区域.我希望添加的是由媒体市场变量"MMarket"定义的区域周围的粗体黑线边框,类似于上面显示的地图. fill变量将是Region,媒体市场边界轮廓将使用MMarket定义.这是读取和加强数据的方式:
Essentially, the map shows a region breakdown of Florida counties, with media markets outlined with a bolded-black line border. I am able to plot the regions easily enough. What I am hoping to add is a bolder, black line border around outside of the regions defined by the media market variable "MMarket", similar to that of the map shown above. The fill variable would be Region and the media market border outline would be defined using MMarket. Here is how the data is read in and fortified:
#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
中.这是我正在使用的合并数据帧的示例:
The fl_data
object contains the data to be mapped (including the media market variable) and the shapefile data is read into flcounties1
. Here is a sample of the merged dataframe I'm using:
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
我可以使用以下代码轻松获得region变量的映射:
I'm able to get a map of the region variable pretty easily using the following code:
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))
推荐答案
下面是一个简单的示例,以防您想通过ggplot2::geom_sf
进入sf
.由于我没有您的shapefile,因此我只是使用tigris
下载康涅狄格州的县分区shapefile,然后将其转换为简单的要素对象.
Here's a quick example in case you want to get into sf
with ggplot2::geom_sf
. Since I don't have your shapefile, I'm just downloading the county subdivisions shapefile for Connecticut using tigris
, and then convert it to a simple features object.
更新说明::sf
的最新版本似乎发生了一些变化,因此您现在应该将城镇合并为仅summarise
个县.
Update note: a few things seem to have changed with more recent versions of sf
, such that you should now union the towns into counties with just 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")
如果我想按原样绘制这些城镇,可以使用ggplot
和geom_sf
:
If I want to plot those towns as they are, I can use ggplot
and geom_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
管道,真棒.
Grouping and calling summarise
with no function gives you the union of several features. I'm going to unite towns based on their county FIPS code, which is the COUNTYFP
column. sf
functions fit into dplyr
pipelines, which is awesome.
所以这个:
ct_sf %>%
group_by(COUNTYFP) %>%
summarise()
将给我一个sf
对象,其中所有城镇均已合并到其县中.我可以将两者结合起来以在第一层geom_sf
中获得城镇地图,并在第二层中即时进行县联合:
would give me a sf
object where all the towns have been merged into their counties. I can combine those two to get a map of towns in the first geom_sf
layer, and do the union for counties on the fly in the second layer:
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
!
这篇关于ggplot-在地图顶部创建边框叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!