Lou*_*ell 7 maps r ggplot2 choropleth
因此,我正在尝试根据自定义变量创建带有边界的佛罗里达县级地图。我在此处包含了我正在尝试创建的旧版地图
从本质上讲,该地图显示了佛罗里达州各县的区域细分,媒体市场用粗体黑线边框勾勒。我能够很容易地绘制区域。我希望添加的是在媒体市场变量“MMarket”定义的区域之外的更粗的黑线边框,类似于上面显示的地图。填充变量将为 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)
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码很容易地获得区域变量的地图: 
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))
Run Code Online (Sandbox Code Playgroud)
下面是如果你想进入一个简单的例子sf用ggplot2::geom_sf。由于我没有您的 shapefile,我只是使用 下载康涅狄格州的县细分 shapefile tigris,然后将其转换为简单的要素对象。
更新说明:一些事情似乎在 的更新版本中发生了变化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")
Run Code Online (Sandbox Code Playgroud)
如果我想按原样绘制这些城镇,我可以使用ggplot和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)
Run Code Online (Sandbox Code Playgroud)

summarise没有功能的分组和调用为您提供了多个功能的结合。我将根据他们的县 FIPS 代码来联合城镇,这是COUNTYFP列。sf函数适合dplyr管道,这很棒。
所以这:
ct_sf %>%
group_by(COUNTYFP) %>%
summarise()
Run Code Online (Sandbox Code Playgroud)
会给我一个sf所有城镇都合并到他们县的对象。我可以将这两者结合起来得到第一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)
Run Code Online (Sandbox Code Playgroud)

没有了fortify!
可能有更好的方法来做到这一点,但我的解决方法是在您需要绘制的所有维度上强化数据。
在您的情况下,我将创建您的县和 MMarkets 的强化数据集,并像您一样绘制地图,但再添加一层geom_polygon不填充的图层,以便仅绘制边框。
merged_counties <- fortify(merged, region = "id")
merged_MMarket <- fortify(merged, region = "MMarket")
Run Code Online (Sandbox Code Playgroud)
进而
ggplot() +
# county polygons
geom_polygon(data = merged_counties, aes(fill = Region,
x = long,
y = lat,
group = group)) +
# here comes the difference
geom_polygon(data = merged_MMarket, aes(x = long,
y = lat,
group = group),
fill = NA, size = 0.2) +
# 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))
Run Code Online (Sandbox Code Playgroud)
使用巴西形状文件的示例
brasil <- readOGR(dsn = "path to shape file", layer = "the file")
brasilUF <- fortify(brasil, region = "ID_UF")
brasilRG <- fortify(brasil, region = "REGIAO")
ggplot() +
geom_polygon(data = brasilUF, aes(x = long, y = lat, group = group), fill = NA, color = 'black') +
geom_polygon(data = brasilRG, aes(x = long, y = lat, group = group), fill = NA, color = 'black', size = 2) +
theme(rect = element_blank(), # drop everything and keep only maps and legend
line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank()
) +
labs(x = NULL, y = NULL) +
coord_map()
Run Code Online (Sandbox Code Playgroud)