Home > front end >  Order of bars different in legend than of plot (lollipop chart)
Order of bars different in legend than of plot (lollipop chart)

Time:01-10

I am trying to create a lollipopchart and have so far managed to create the chart but for reasons I cannot understand the order of the legend is opposite that of the bars. lollipop chart

Using the following code to produce the chart.

DATA

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"))

CHART

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()

Everything plots fine but as said, the order for the legend is reversed.

I have tried fidelling with scale_x_continuous, factor levels and trying to get position = position_dodge2(reverse = TRUE) to work, but have failed.

CodePudding user response:

One option to achieve the same order would be to reverse the order of the legend using 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))

enter image description here

Or as a second option reverse the order of the grouping column using e.g. group=forcats::fct_rev(rubber) globally inside ggplot():

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()

enter image description here

  • Related