You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

棒棒糖图(lollipop chart)图例与图表元素顺序不一致问题

棒棒糖图图例顺序与图表元素顺序相反问题解决

问题描述

已成功用ggplot2生成棒棒糖图,但图例顺序与图表中的元素顺序相反。尝试调整scale_x_continuous、因子水平,以及使用position_dodge2(reverse = TRUE)均未解决。

所用数据

structure(list(rubber = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("-165", "-170", 
"-171", "-172", "-173"), class = "factor"), name = c("T10", "T90", 
"Ml", "Mh", "Hardness, plader", "Tensile Strength", "Elongation at Break", 
"100% Modul", "Tear Strength", "Density", "Mold shrinkage (DeMeet)", 
"23 °C, 72 h, 25%, 30 min", "70 °C, 24 h, 25%, 30 min", "T10", 
"T90", "Ml", "Mh", "Hardness, plader", "Tensile Strength", "Elongation at Break", 
"100% Modul", "Tear Strength", "Density", "Mold shrinkage (DeMeet)", 
"23 °C, 72 h, 25%, 30 min", "70 °C, 24 h, 25%, 30 min", "T10", 
"T90", "Ml", "Mh", "Hardness, plader", "Tensile Strength", "Elongation at Break", 
"100% Modul", "Tear Strength", "Density", "Mold shrinkage (DeMeet)", 
"23 °C, 72 h, 25%, 30 min", "70 °C, 24 h, 25%, 30 min", "T10", 
"T90", "Ml", "Mh", "Hardness, plader", "Tensile Strength", "Elongation at Break", 
"100% Modul", "Tear Strength", "Density", "Mold shrinkage (DeMeet)", 
"23 °C, 72 h, 25%, 30 min", "70 °C, 24 h, 25%, 30 min", "T10", 
"T90", "Ml", "Mh", "Hardness, plader", "Tensile Strength", "Elongation at Break", 
"100% Modul", "Tear Strength", "Density", "Mold shrinkage (DeMeet)", 
"23 °C, 72 h, 25%, 30 min", "70 °C, 24 h, 25%, 30 min"), value = c(0.73, 
6.7, 1.49, 14.4, 70.33333333, 16.95, 436, 4.02, 26, 1.1103, 2.179768, 
8.1, 18.6, 0.76, 7.68, 1.58, 14.24, 71.7, 17.82, 461, 3.87, 28.45, 
1.1091, 2.157857, 8.9, 19.3, 0.82, 9.8, 1.62, 12.96, 70.23333333, 
17.73, 526, 3.58, 32.81, 1.11, 2.356138, 12, 24.1, 0.79, 7.85, 
1.23, 13.28, 69.7333333, 15.6, 468, 3.63, 27.85, 1.1093, 2.19892, 
9, 25.5, 0.7, 7.66, 1.11, 13.67, 70, 15.68, 416, 3.9, 26.48, 
1.10696712, 2.117513, 7.8, 18)), row.names = c(NA, -65L), class = c("tbl_df", 
"tbl", "data.frame"))

绘图代码

lollipop_plot <- plot_dat |> 
  ggplot(aes(x = name, y = value)) +
  geom_linerange(
    aes(
      xmin = name,
      xmax = name,
      ymin = 0,
      ymax = value,
      group = rubber
    ),
    position = position_dodge(width = 0.9),
    colour = "darkgrey",
    alpha = 0.9
  ) +
  geom_point(aes(col = rubber),
             position = position_dodge(width = 0.9),
             size = 2.5
  ) +
  scale_y_log_eng() +
  labs(
    x = NULL,
    y = "Value",
    colour = "Mix",
    caption = "-173 is 50/50 mix of polymer"
  ) +
  coord_flip() +
  theme_bw()

解决方案

方法1:直接反转图例顺序

在绘图代码中添加scale_colour_discrete(reverse = TRUE),让图例顺序和图表元素顺序匹配:

lollipop_plot <- plot_dat |> 
  ggplot(aes(x = name, y = value)) +
  geom_linerange(
    aes(
      xmin = name,
      xmax = name,
      ymin = 0,
      ymax = value,
      group = rubber
    ),
    position = position_dodge(width = 0.9),
    colour = "darkgrey",
    alpha = 0.9
  ) +
  geom_point(aes(col = rubber),
             position = position_dodge(width = 0.9),
             size = 2.5
  ) +
  scale_y_log_eng() +
  scale_colour_discrete(reverse = TRUE) # 新增此行反转图例
  labs(
    x = NULL,
    y = "Value",
    colour = "Mix",
    caption = "-173 is 50/50 mix of polymer"
  ) +
  coord_flip() +
  theme_bw()

方法2:调整因子水平并反转dodge位置

如果希望保留图例原始顺序,调整图表内元素的排列顺序:

  1. 先反转rubber的因子水平:
plot_dat$rubber <- factor(plot_dat$rubber, levels = rev(levels(plot_dat$rubber)))
  1. 绘图时给position_dodge添加reverse = TRUE参数:
lollipop_plot <- plot_dat |> 
  ggplot(aes(x = name, y = value)) +
  geom_linerange(
    aes(
      xmin = name,
      xmax = name,
      ymin = 0,
      ymax = value,
      group = rubber
    ),
    position = position_dodge(width = 0.9, reverse = TRUE), # 反转dodge顺序
    colour = "darkgrey",
    alpha = 0.9
  ) +
  geom_point(aes(col = rubber),
             position = position_dodge(width = 0.9, reverse = TRUE), # 反转dodge顺序
             size = 2.5
  ) +
  scale_y_log_eng() +
  labs(
    x = NULL,
    y = "Value",
    colour = "Mix",
    caption = "-173 is 50/50 mix of polymer"
  ) +
  coord_flip() +
  theme_bw()

两种方法均可解决问题,方法1更简洁高效。

内容的提问来源于stack exchange,提问作者Godrim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 09:05:30