r - 如何使用 aes、ggplot2 和地图添加图例?
问题描述
我正在尝试制作一张为加拿大草原省份绘制点的地图,但我没有运气在我的地图中添加图例。我对 r 中的映射非常陌生,所以我不明白我应该如何包含 aes 来获得图例。我的 siteDataTrees 数据来自 excel csv 文件,顶部如下所示: siteDataTrees 和 siteDataBoth 的数据也来自 csv 文件,顶部如下所示: siteDataBoth。
这是我到目前为止的代码:
library(maps)
library(ggplot2)
library(sf)
prairies1 <- map("worldHires","Canada", xlim = c(-120,-87), ylim = c(49,61),
plot = FALSE, fill = TRUE)
prairies <- st_as_sf(prairies1)
ggplot(data = prairies) +
geom_sf() +
geom_point(data = siteDataTrees, aes(x = long, y = lat), size = 2.5, pch = 21,
fill = "purple", show.legend = TRUE) +
geom_point(data = siteDataBoth, aes(x = long, y = lat), size = 2.5, pch = 21,
fill = "light green", show.legend = TRUE) +
geom_text(data = locations, aes(x = long, y = lat, label = name),
size = 2, col = "black", check_overlap = FALSE) +
annotation_scale(location = "tr", width_hint = 0.2) +
ggtitle("Climate Stations and Tree Chronology Locations for South AB") +
labs(x = "latitude", y = "longitude") +
theme(legend.position = "right") +
coord_sf(xlim = c(-115, -110), ylim = c(48.9, 50.49), expand = FALSE)
我还包括了一张地图,以显示没有图例的情况。
我应该如何获取数据框大草原并将其与 aes 一起使用以包含图例?是否有另一种方法可以在不使用 aes 函数的情况下在 ggplot2 中添加图例?提前感谢您的帮助,如果缺少某些内容,请告诉我,因为这是我的第一次发布
解决方案
让我给你几个例子,说明如何使用 r-spatial 的一个稍微修改的例子来计算一个图例。
首先我们准备数据:
library(maps)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
(sites <- data.frame(longitude = c(-80.144005, -80.109),
latitude = c(26.479005,26.83),
type = c("tree", "station")))
现在我们绘制。案例1:颜色不是问题
ggplot(data = world) +
geom_sf() +
geom_point(data = sites,
aes(x = longitude, y = latitude, fill = type),
size = 4,
shape = 23) +
coord_sf(xlim = c(-88, -78), ylim = c(24.5, 33), expand = FALSE) +
theme(legend.position = "bottom")
案例2:填充颜色是一个问题。在这里,我们可以使用命名向量通过点类型传递我们想要的颜色和标签。例如:
mapfill <- c('tree' = "forestgreen", 'station' = "purple")
maplab <- c('tree' = "trees in prairies", 'station' = "Stations in prairies")
然后我们绘制结合两者mapfill
和maplab
:
ggplot(data = world) +
geom_sf() +
geom_point(data = sites, aes(x = longitude, y = latitude, fill = type), size = 4,
shape = 23) +
scale_fill_manual(values = mapfill, labels = maplab) +
coord_sf(xlim = c(-88, -78), ylim = c(24.5, 33), expand = FALSE) +
theme(legend.position = "bottom")
备注 1如果您不想输入图例标题,您可以使用 inside 删除legend. title = element_blank()
它theme
备注 2如果您使用的是color而不是fill,请使用 function 。如果您将填充和颜色组合在一起,请执行相同的操作scale_color_manual
scale_***_manual
本着全面披露的精神,如果您不介意颜色并且想要快速修复(我不能强调这一点),您也可以fill = "TextYouWantInLegend"
在aes
. 请参见以下示例:
ggplot(data = world) +
geom_sf() +
geom_point(data = sites[1,], aes(x = longitude, y = latitude, fill = "toto"), size = 4,
shape = 23) +
geom_point(data = sites[2,], aes(x = longitude, y = latitude, fill = "koko"), size = 4,
shape = 23) +
coord_sf(xlim = c(-88, -78), ylim = c(24.5, 33), expand = FALSE) +
theme(legend.position = "bottom")
推荐阅读
- php - 更改数组元素的值,Laravel
- java - 扫描 10 个数字,将它们存储在一个数组中,然后将数组反转
- python - 如何通过python中的selenium加载网站的所有评论
- android-studio - 对该常量表达式的求值会抛出一个表达式
- java - 设置 SpigotMC 会导致崩溃并询问 Git 详细信息?
- css - 使用给定 CSS 的底部菜单在我的 WordPress 项目中不起作用
- javascript - 由于安全错误,Github 文件未呈现?
- ruby-on-rails - 如何使用sidekiq-scheduler同步cron作业同时执行
- docker - 无法在 ElasticSearch 上加载和使用 synonym.txt 文件
- google-apps-activity - 过滤动作事件不准确