【问题标题】:How do I add a legend using aes, ggplot2 and maps?如何使用 aes、ggplot2 和地图添加图例?
【发布时间】:2020-06-30 19:49:06
【问题描述】:

我正在尝试制作一张为加拿大草原省份绘制点的地图,但我没有运气在我的地图中添加图例。我对 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 中添加图例?提前感谢您的帮助,如果有什么遗漏请告诉我,因为这是我第一次发帖

【问题讨论】:

  • 您希望您的图例显示什么?使用 ggplot2 创建图例 fill 和/或 color 应包含在 aes 中。您可能会发现以下链接很有用:r-spatial.org/r/2018/10/25/ggplot2-sf.html
  • 嗨 davidnortes,感谢您的回复!我试图在图例中包含紫色点和绿色点。感谢您提供该链接,我使用它相当多,但不幸的是我并不完全了解如何使用草原来获得那个传说。
  • 嗨 davidnortes,感谢您的帮助,因为我能够解决我的错误!我在错误的代码行中使用了 aes 并且没有正确使用颜色

标签: r ggplot2 rmaps


【解决方案1】:

让我举几个例子,说明如何使用 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如果您不想在图例标题中键入,您可以在theme 中使用legend. title = element_blank() 将其删除

备注 2 如果您使用 color 而不是 fill,请使用函数 scale_color_manual。如果您同时结合 fill 和 color 对 scale_***_manual

执行相同操作

本着全面披露的精神,如果您不介意颜色并且想要快速修复(我不能强调这一点),您还可以在 aes 内编码 fill = "TextYouWantInLegend"。请参见以下示例:

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")

【讨论】:

  • 嗨 davidnortes,是的,非常感谢您的帮助 :) 看到代码帮助我找到了我的错误,因为我将 aes 函数放在了错误的行中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2020-12-03
  • 2018-05-11
相关资源
最近更新 更多