将图例对象缩放至相同大小和对齐方式

问题描述 投票:0回答:1

我如何调整图例,使图例标志和文本都在相同的边缘宽度/框中缩放?我希望它们垂直和水平对齐,每个标志的尺寸相似。我知道我可以手动调整它们的各种 theme() 选项,但我假设必须有一种方法来修复所有图例项目的一般尺寸,使它们具有相同的整体尺寸,从而使它们正确对齐。

数据

may_sonde_do <- structure(list(station_name = c("TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", 
"TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", 
"TMDL-R2", "TMDL-R3", "TMDL-R4", "TMDL-R1", "TMDL-R2", "TMDL-R3", 
"TMDL-R4", "TMDL-R1", "TMDL-R2"), sample_date_time = c("2023-05-12 11:00:00", 
"2023-05-12 11:00:00", "2023-05-12 11:00:00", "2023-05-12 11:00:00", 
"2023-05-12 11:15:00", "2023-05-12 11:15:00", "2023-05-12 11:15:00", 
"2023-05-12 11:15:00", "2023-05-12 11:30:00", "2023-05-12 11:30:00", 
"2023-05-12 11:30:00", "2023-05-12 11:30:00", "2023-05-12 11:45:00", 
"2023-05-12 11:45:00", "2023-05-12 11:45:00", "2023-05-12 11:45:00", 
"2023-05-12 12:00:00", "2023-05-12 12:00:00", "2023-05-12 12:00:00", 
"2023-05-12 12:00:00", "2023-05-12 12:15:00", "2023-05-12 12:15:00", 
"2023-05-12 12:15:00", "2023-05-12 12:15:00", "2023-05-12 12:30:00", 
"2023-05-12 12:30:00", "2023-05-12 12:30:00", "2023-05-12 12:30:00", 
"2023-05-12 12:45:00", "2023-05-12 12:45:00"), parameter = c("Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen"), result = c(8.42, 8.82, 8.94, 9.77, 8.42, 
8.74, 8.9, 9.39, 8.37, 8.68, 8.85, 8.73, 8.37, 8.6, 8.76, 8.44, 
8.33, 8.56, 8.7, 8.26, 8.31, 8.48, 8.63, 8.07, 8.31, 8.42, 8.57, 
7.85, 8.31, 8.34), units = c("mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L"), comments = c(NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA), date = c("2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", "2023-05-12", 
"2023-05-12", "2023-05-12", "2023-05-12"), ym = c("2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
"2023-05", "2023-05", "2023-05", "2023-05", "2023-05")), row.names = c(NA, 
30L), class = "data.frame")

may_sonde_do_situ <- structure(list(station_name = c("Instant TMDL-R1", "Instant TMDL-R1", 
"Instant TMDL-R1", "Instant TMDL-R2", "Instant TMDL-R2", "Instant TMDL-R2", 
"Instant TMDL-R3", "Instant TMDL-R3", "Instant TMDL-R3", "Instant TMDL-R4", 
"Instant TMDL-R4", "Instant TMDL-R4"), sample_date_time = c("2023-05-12 06:17:00", 
"2023-05-19 04:42:00", "2023-05-26 04:55:00", "2023-05-12 05:26:00", 
"2023-05-19 04:11:00", "2023-05-26 04:20:00", "2023-05-12 04:30:00", 
"2023-05-19 03:33:00", "2023-05-26 03:25:00", "2023-05-12 03:36:00", 
"2023-05-19 02:40:00", "2023-05-26 02:45:00"), parameter = c("Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen", 
"Dissolved Oxygen", "Dissolved Oxygen", "Dissolved Oxygen"), 
    result = c(9.81, 8.46, 9.77, 9.68, 8.41, 10.62, 10.33, 8.81, 
    9.19, 9.57, 7.89, 8.86), units = c("mg/L", "mg/L", "mg/L", 
    "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
    "mg/L"), date = c("2023-05-12", "2023-05-19", "2023-05-26", 
    "2023-05-12", "2023-05-19", "2023-05-26", "2023-05-12", "2023-05-19", 
    "2023-05-26", "2023-05-12", "2023-05-19", "2023-05-26"), 
    ym = c("2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
    "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", "2023-05", 
    "2023-05")), row.names = c(NA, -12L), class = "data.frame")

代码

annotation_do_sonde <- data.frame(
   x = c("2023-05-22"),
   y = c(4),
   label = c("Numeric Target Minimum DO = 7 mg/L")
)



ggplot() +
    geom_point(data = may_sonde_do, aes(x = sample_date_time,
                                      y = result, color = station_name,
                                      group = station_name),
                 size = 0.8, alpha = 0.6) +
  geom_point(data = may_sonde_do_situ, aes(x = sample_date_time, y = result, fill = station_name,
                                           shape = station_name,
                                           group = station_name), size = 3) +
  geom_point(data = may_sonde_do_situ, aes(x = sample_date_time, y = result, 
                                           shape = station_name,
                                           group = station_name), size = 3,
                color  = 'black', alpha = 1) +

  scale_y_continuous(limits = c(0, 20), breaks = seq(0, 20, by = 2),
                     expand = c(0,0)) +
  scale_x_discrete(breaks = unique(may_sonde_do$date),
                   labels = unique(may_sonde_do$date),
                   expand = c(0.04,0.04)) +
  scale_shape_manual(values=22:26)  +
  geom_hline(yintercept = c(7), linetype = "solid", color = "black", size = 0.7) +
  scale_color_viridis(
    discrete = TRUE, name = NULL,
    guide = guide_legend(order = 2)
  ) +
  scale_fill_viridis_d() +
  labs(title = "May 2023 - Dissolved Oxygen", y = "Dissolved Oxygen (mg/L)") +  
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5),
    axis.title.x = element_blank(),
    legend.position = "bottom",
    legend.box = "vertical",
    legend.direction = "horizontal",
    panel.grid.major.y = element_line(size = .01, color = "grey60"),
    legend.margin = margin(),
    panel.grid.major.x = element_line(size=.01, color="grey60" ),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.title=element_blank(),
    legend.text = element_text(size = 6.7)) +
    geom_text(data = annotation_do_sonde, aes(x=x, y=y, label=label), size = 3.5, size.unit = "mm")

Legend aligment is off

r ggplot2
1个回答
0
投票

实现所需结果的一种选择是使用

override.aes
guide_legend
参数将
size
图例符号的
color
设置为与用于
fill
图例的符号相同。其次,为了对齐符号,我使用了一个小技巧,将透明字符串
" Instant"
添加到颜色图例的标签。这样,图例标签的长度与填充图例的长度相同。为了使添加的字符串透明,我使用了
ggtext::element_markdown
。这当然不是通用选项,但适用于您的用例。

library(ggplot2)
library(viridis)

ggplot() +
  geom_point(
    data = may_sonde_do, aes(
      x = sample_date_time,
      y = result, color = station_name,
      group = station_name
    ),
    size = 0.8, alpha = 0.6
  ) +
  geom_point(data = may_sonde_do_situ, aes(
    x = sample_date_time, y = result, fill = station_name,
    shape = station_name,
    group = station_name
  ), size = 3) +
  geom_point(
    data = may_sonde_do_situ, aes(
      x = sample_date_time, y = result,
      shape = station_name,
      group = station_name
    ), size = 3,
    color = "black", alpha = 1
  ) +
  geom_hline(yintercept = c(7), linetype = "solid", color = "black", size = 0.7) +
  geom_text(
    data = annotation_do_sonde, aes(x = x, y = y, label = label),
    size = 3.5, size.unit = "mm"
  ) +
  scale_y_continuous(
    limits = c(0, 20), breaks = seq(0, 20, by = 2),
    expand = c(0, 0)
  ) +
  scale_x_discrete(
    breaks = unique(may_sonde_do$date),
    labels = unique(may_sonde_do$date),
    expand = c(0.04, 0.04)
  ) +
  scale_shape_manual(values = 22:26) +
  scale_color_viridis(
    labels = \(x) paste0(x, "<span style='color: transparent'> Instant</span>"),
    discrete = TRUE, name = NULL,
    guide = guide_legend(order = 2, override.aes = list(size = 3))
  ) +
  scale_fill_viridis_d() +
  labs(title = "May 2023 - Dissolved Oxygen", y = "Dissolved Oxygen (mg/L)") +
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5),
    axis.title.x = element_blank(),
    legend.position = "bottom",
    legend.box = "vertical",
    legend.direction = "horizontal",
    panel.grid.major.y = element_line(size = .01, color = "grey60"),
    legend.margin = margin(),
    panel.grid.major.x = element_line(size = .01, color = "grey60"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.title = element_blank(),
    legend.text = ggtext::element_markdown(size = 6.7)
  )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.