更改 ggplot 图例标签的位置和对齐方式

Iqb*_*yar 1 r ggplot2

我最近访问了《经济学人》,看到了一篇关于 Wordl 的未成年婚姻的地图可视化的帖子。你可以在这里看到帖子

我正在尝试使用 R 语言中的 ggplot2 重新制作情节,但在制作图例时发现了一些困难。

这是我目前能做的

我的_ggplot

这是我的代码来可视化它

library(ggplot2)
library(dplyr)

WorldData <- map_data('world') %>% filter(region != "Antarctica") %>% fortify

p <- ggplot() +
    geom_map(data = WorldData, map = WorldData,
                  aes(x = long, y = lat, group = group, map_id=region),
                  fill = "#b4c3c9", colour = "white", size=0.05) + 

    geom_map(data = child_marriage, map=WorldData,
                  aes(fill=Total, map_id=Country),
                  colour="white", size=0.05) +

      coord_map(projection = "mollweide",ylim=c(-90,90), xlim=c(-180,180)) +

    scale_fill_continuous(low="#f1dcbb", high="#005379", guide = guide_legend()) + #gonna change this
    scale_colour_manual(values = c("red", "blue", "green")) + 

    scale_y_continuous(breaks=c()) +
    scale_x_continuous(breaks=c()) +

    labs(
       title = "Girlhood, Interupted",
       fill="Marriages",
       subtitle = "Women aged 20-24 who were married before age 18,2010-17, %",
       caption = "Source: UNICEF",
       x = NULL,
       y = NULL
       ) +

    theme(
          plot.margin = margin(0,15,0,15),
          panel.background = element_rect(fill = "white"),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_line(color = "darkgrey"),
          legend.text = element_text(margin = margin(l=3), size = 10),
          legend.title = element_blank(),
          legend.position = c(0.8,1),
          legend.direction = "horizontal", 
          legend.key.width = unit(20,"pt"),
          legend.key.height = unit(10, "pt"),
          axis.text = element_text(size = rel(1), color = "gray8"),
          axis.line.x  = element_line(color = "gray8"),
          axis.ticks.y = element_blank(),
          plot.title = element_text(size = rel(1.3), hjust = 0, face = "bold"),
          plot.subtitle = element_text(size = rel(0.8), hjust = 0, face = "bold"),
          plot.caption = element_text(hjust = 0, size = 8, color="#b4c3c9"))
p 
Run Code Online (Sandbox Code Playgroud)

我想让传说像原来的帖子一样,这个

原帖

任何想法将不胜感激

cam*_*lle 5

以下是我根据您的代码的简化版本和一些虚拟数据提出的两个想法。这两种选择都需要大量的修补才能使它们正确 - 我将大部分留给您。

library(ggplot2)
library(dplyr)
library(patchwork)

WorldData <- map_data('world') %>% 
  filter(region != "Antarctica") %>% 
  fortify()

set.seed(1234)
child_marriage <- tibble(Country = unique(WorldData$region),
                         Total = runif(length(Country), 0, 100))

marr_map <- ggplot() +
  geom_map(aes(x = long, y = lat, group = group, map_id = region),
           data = WorldData, map = WorldData) +
  geom_map(aes(fill = Total, map_id = Country),
           data = child_marriage, map = WorldData) +
  scale_fill_continuous(breaks = seq(0, 100, by = 20), name = NULL,
                        guide = guide_legend(
                          label.position = "top", 
                          label.hjust = 1,
                          override.aes = list(size = 0))
                        )
Run Code Online (Sandbox Code Playgroud)

选项 1 涉及对与图例相关的主题元素和参数的调整guide_legend。您可以将图例的位置调整到右上角或任何地方,但您在图例键、标签等的间距和对齐方式的一些细节方面受到限制。

marr_map +
  theme(
    legend.position = "top",
    legend.direction = "horizontal",
    legend.key.width = unit(25, "pt"),
    legend.key.height = unit(10, "pt"),
    legend.spacing.x = unit(2, "pt"),
    legend.text = element_text(margin = margin(0, 0, 0, 0, "pt"), size = 10)
  )
Run Code Online (Sandbox Code Playgroud)

选项 2 可能是也可能不是矫枉过正,但它将图例视为一个单独的小图,然后用于patchwork将其粘贴到地图上。你有更多的控制权,但它变得非常乏味。由于在示例中,我以 20 的间隔放置图例键,因此我将文本调整为向上凸起 9 个单位,以便它在键的右侧附近排列(geom_tilerects 为中心)。这geom_segment为您提供了从键延伸到标签的小刻度线,就像《经济学人》使用的那样。

legend_df <- tibble(y = 1, Total = seq(20, 80, by = 20))

map_legend <- ggplot(legend_df, aes(x = Total, y = y, fill = Total)) +
  geom_tile(color = "white", size = 1.5) +
  geom_text(aes(label = Total), 
            hjust = 1, nudge_y = 9, nudge_x = 0.8, size = 3) +
  geom_segment(aes(x = Total + 10, xend = Total + 10, y = 0.5, yend = 2)) +
  # coord_flip() +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  labs(x = NULL, y = NULL) +
  theme(legend.position = "none",
        panel.background = element_blank())

marr_no_legend <- marr_map + theme(legend.position = "none")

{( map_legend | plot_spacer() ) + plot_layout(widths = c(1, 1.4))} /
  marr_no_legend +
  plot_layout(heights = c(1, 12))
Run Code Online (Sandbox Code Playgroud)

我没有弄清楚的一件事是将图例放在右侧,左侧有一个间隔器。当我plot_spacer()在图例之前对齐时,对齐会变得一团糟,我找不到错误报告或patchwork与之相关的任何内容。