簡體   English   中英

如何使用 aes、ggplot2 和地圖添加圖例?

[英]How do I add a legend using aes, ggplot2 and maps?

我正在嘗試制作一個 map,並為加拿大草原省份繪制點,但我沒有運氣在我的 map 中添加一個圖例。 我對 r 中的映射非常陌生,所以我不明白我應該如何包含 aes 來獲得圖例。 我的 siteDataTrees 數據來自 excel csv 文件,頂部看起來像這樣: siteDataTrees和 siteDataBoth 的數據也來自csv文件,頂部看起來像這樣:

這是我到目前為止的代碼:

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)

我還包括了一個 map 來展示沒有圖例的樣子。
地圖
我應該如何獲取數據框大草原並將其與 aes 一起使用以包含圖例? 是否有另一種方法可以在不使用 aes function 的情況下在 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")))

現在我們 plot。 案例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 :填充顏色是一個問題。 在這里,我們可以使用命名向量來傳遞 colors 和我們想要的點類型的標簽。 例如:

mapfill <- c('tree' = "forestgreen", 'station' = "purple")
maplab <- c('tree' = "trees in prairies", 'station' = "Stations in prairies")

然后我們 plot 結合mapfillmaplab

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如果您不想輸入圖例標題,您可以使用legend. title = element_blank() theme內的legend. title = element_blank()

備注 2如果您使用的是color而不是fill ,請使用 function scale_color_manual 如果您同時使用填充顏色,請使用scale_***_manual

本着全面披露的精神,如果您不介意 colors 並且想要快速修復(我不能強調這一點),您也可以在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")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM