【问题标题】:Why doesn't my legend appear when drawing geom_sf with ggplot?为什么用ggplot绘制geom_sf时我的图例没有出现?
【发布时间】:2021-03-23 19:25:51
【问题描述】:

我正在关注this guide 在地图上创建一些点。一切都很好,但我无法为这些点制作图例。我只是要复制代码以获得 MWE(但所有功劳归于 Valentin Stefan):

library(rgeos)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(dplyr)
library(ggplot2)
library(ggrepel)

theme_set(
  theme_bw() +
    theme(panel.background = element_rect(fill = "azure"),
          panel.grid.major = element_blank(),
          axis.title = element_blank(),
          axis.text = element_text(size = 8))
)

world <- rnaturalearth::ne_countries(scale = 'medium', returnclass = "sp")
box_cut <- bbox2SP(n = 90, s = -90, w = -70, e = 120, proj4string = world@proj4string)
world_crop <- gDifference(world, box_cut)

pacific_crop <- world_crop %>% 
  st_as_sf() %>% # change from sp to sf object/class
  st_shift_longitude() %>% 
  st_crop(c(xmin = st_bbox(.)[["xmin"]],
            xmax = st_bbox(.)[["xmax"]],
            ymin = -50,
            ymax = 30))

ggplot() +
  geom_sf(data = pacific_crop)

tiny_countries <- rnaturalearthdata::tiny_countries50 %>% 
  st_as_sf() %>%
  st_shift_longitude() %>% 
  st_crop(c(xmin = 120, xmax = 250, ymin = -50, ymax = 30)) %>% 
  # Also adds the coordinates to be used for labeling with geom_text_repel
  bind_cols(st_coordinates(.) %>% as.data.frame())

rbPal <- colorRampPalette(c('red','blue'))

Col <- rbPal(18)

ggplot() +
  geom_sf(data = pacific_crop) +
  geom_sf(data = tiny_countries, size = 2, color = Col, show.legend = "point")

如何获得颜色图例?

我试过改变图例的位置,但没有成功:

theme(legend.position = c(.5,.5))

【问题讨论】:

    标签: r ggplot2 sf


    【解决方案1】:

    当您的数据集中的一个或多个列通过aes() 映射到美学修饰符之一(例如,fillshapesizecoloralpha...)。在您的绘图代码中,您指定了一个颜色值列表,但ggplot2 不知道应该在您的数据中链接到什么。因此,颜色是根据您的数据框tiny_countries 中的观察结果逐行映射的,而实际上并未在您的数据和颜色之间生成链接。

    当您将color 放入aes() 中时,您表示该修饰符应映射到数据中的特定列。 ggplot2 然后将为数据框中的每个“不同”值分配颜色。如果您只想ggplot2 指定每个小国家是不同的颜色,那么您需要为每个国家选择一个标签。我们自然希望标签与名称相对应,因为...这是有道理的,所以我们将通过 aes(color=name) 创建映射。

    请注意,这并没有指定颜色。默认情况下,ggplot2 将使用它的默认配色方案,但由于您已经为颜色指定了适当数量的值列表...我们可以告诉ggplot2 我们想要覆盖默认颜色并使用您自己的特定颜色手动上色。我们使用scale_*_manual() 函数来做到这一点,其中* 对应于所讨论的特定美学修饰符。把这些放在一起你会得到这个:

    ggplot() +
      geom_sf(data = pacific_crop) +
      geom_sf(data = tiny_countries, aes(color=name), size = 2, show.legend = "point") +
      scale_color_manual(values=Col) +
      theme(legend.position='bottom')  # thought the legend looks best on the bottom
    

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 1970-01-01
      • 2021-07-02
      • 2019-01-04
      • 2021-09-03
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多