R语言 图例中条形的顺序与图中条形的顺序不同(棒棒糖图)

plupiseo  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(170)

我正在尝试创建一个棒棒糖图表,到目前为止已经成功地创建了图表,但由于我无法理解的原因,图例的顺序与条形相反。

使用以下代码生成图表。

    • 数据**
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()

一切情节罚款,但如前所述,顺序的图例是颠倒的。
我试过fidling与scale_x_continuous,因子水平,并试图让position = position_dodge2(reverse = TRUE)的工作,但都失败了。

x7yiwoj4

x7yiwoj41#

实现相同顺序的一个选项是使用guide_legend(reverse=TRUE)反转图例的顺序:

library(ggplot2)

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_log10() +
  labs(
    x = NULL,
    y = "Value",
    colour = "Mix",
    caption = "-173 is 50/50 mix of polymer"
  ) +
  coord_flip() +
  theme_bw() +
  guides(color = guide_legend(reverse = TRUE))

或者作为第二选项,例如使用ggplot()内的全局group=forcats::fct_rev(rubber)来反转分组列的顺序:

plot_dat |> 
  ggplot(aes(x = name, y = value, group = forcats::fct_rev(rubber))) +
  geom_linerange(
    aes(
      xmin = name,
      xmax = name,
      ymin = 0,
      ymax = value
    ),
    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_log10() +
  labs(
    x = NULL,
    y = "Value",
    colour = "Mix",
    caption = "-173 is 50/50 mix of polymer"
  ) +
  coord_flip() +
  theme_bw()

相关问题