r - 关于点和多边形的叶绿素的次要图例,ggplot
问题描述
我有一张带有多边形和点的地图 - 显示了世界上感兴趣的国家。
我想要这两个项目(点和多边形)的图例,但不能添加它们。首先绘制多边形(图例也是如此),而点不会出现在图例上。为了尝试解决这个问题,我添加
show.legend = T
然而,图例随后在多边形颜色顶部添加点,如下所示:
我想要的是另一个带有黄点的图例项目,我可以在其中设置我想要的标签。
目前我正在使用单独的文件生成点层。也许我需要从一个包含点和多边形的 df 来完成这一切 - 从单个 aes 生成点和多边形。但鉴于我的积分没有组号,我想不出该怎么做。
这是我的代码:
world <- map_data("world")
countries <- read_excel("country_table.xlsx", sheet = 3) #table of coutries with interest
world3 <- merge(world, countries, all.x = TRUE) %>%
arrange(order)
world4 <- world3 %>%
filter(!is.na(interest))
city <- read_excel("country_table.xlsx", sheet = 4) #point data
city$long <- as.numeric(city$long)
city$lat <- as.numeric(city$lat)
ggplot() +
geom_polygon(data = world3, aes(x = long, y = lat, group = group),
fill = "light grey") +
geom_polygon(data = world4, aes(x = long, y = lat, group = group, fill = interest),
col = "white") +
scale_fill_manual(name = "Interest/Support:",
breaks = c("interest", "past", "current"),
values = c(interest = "#a7ef88", past = "#3a7f1d", current = "#1b5104"),
labels = c("interest", "past", "current")) +
theme_map() +
theme(legend.position = "bottom") +
coord_fixed(xlim = c(-130, 160),
ylim = c(-50, 75),
ratio = 1.4) +
geom_point(data = city, aes(x= long, y = lat),
shape = 21, inherit.aes = F, size = 2, col = "black", fill = "yellow", show.legend = T)
有什么想法吗?
解决方案
下面发布的 ggplot 部分的最终代码。感谢奥史密斯。
ggplot() +
#create base plot all polygons in grey
geom_polygon(data = world3, aes(x = long, y = lat, group = group),
fill = "light grey") +
#create chloropleth layer for countries with data
geom_polygon(data = world4, aes(x = long, y = lat, group = group, fill = interest),
col = "white") +
#add map theme from ggthemes
theme_map() +
#Set the zoom
coord_fixed(xlim = c(-130, 160),
ylim = c(-50, 75), ratio = 1.4) +
#Add city layer - include col in aes() to get city as a separate legend item
geom_point(data = city, aes(x= long, y = lat, col = interest),
shape = 21, inherit.aes = F, size = 3, fill = "yellow") +
#set fill for countries by interest (include city (special) to have the correct number of aesthetics)
scale_fill_manual(name = NULL,
breaks = c("interest", "past", "current", "special"),
values = c(interest = "#a7ef88", past = "#3a7f1d", current = "#1b5104", special = "yellow"),
labels = c("interest", "past", "current", "city")) +
#set color for cities and labels for cities legend
scale_color_manual(name = NULL,
breaks = c("special"),
values = c(special = "black"),
labels = c("cities")) +
#set order of legend items (fill first)
guides(fill = guide_legend(order = 1), color = guide_legend(order = 2)) +
#set legend position and vertical arrangement
theme(legend.text = element_text(size = 9), legend.position = "bottom", legend.box = "vertical")
给出以下内容:
推荐阅读
- docker - Logrotate 不适用于每日和大小
- android - Android Jetpack Compose:如何显示字符串资源中的样式文本
- python - 创建 docker 和 anaconda
- java - 如何将数字分配给数组中的元素
- python - Pandas 按组过滤最小
- rest-assured - 如果我知道 baseURI 和 basePath,是否可以在访问页面时获取参数?
- php - PHP 致命错误:找不到类 PDO (mysql/Docker)
- reactjs - Hijri Material UI 日期选择器中显示的月份是公历
- jenkins-pipeline - 具有条件代理类型的 Jenkins 流水线矩阵
- javascript - 如何显示/隐藏
- 使用 JavaScript 基于其文本内容?