ggplot2 的 geom_errorbar 按降序排列多个变量

Adr*_*ian 5 visualization r ggplot2

library(ggplot2)
mydat <- data.frame(mean = c(23, 24, 15, 27, 18, 19, 23, 20, 32),
           lower = c(20, 19, 13, 15, 14, 18, 20, 17, 20),
           upper = c(25, 29, 17, 39, 22, 20, 26, 23, 40),
           class = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
           domain = c("North", "North", "North", "West", "West", "West", "South", "South", "South"))
mydat$class <- as.factor(mydat$class)
mydat$domain <- as.factor(mydat$domain)

mydat %>% ggplot(aes(x = mean, y = class, color = domain, Group = domain, xmin = lower, xmax = upper)) +
  geom_errorbar(width = 0.1, position = position_dodge(width = 0.5)) + theme_light() + geom_point(position = position_dodge(width = 0.5)) + xlab("Counts")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想通过减小upper每个 s 内class和跨s 的误差条上限(即 )的值来排序domain。期望的输出是这样的:

在此输入图像描述

我尝试了以下代码,但没有达到预期的效果:

library(forcats)
mydat %>% ggplot(aes(x = mean, y = class, color = fct_infreq(domain), Group = fct_infreq(domain), xmin = lower, xmax = upper)) +
  geom_errorbar(width = 0.1, position = position_dodge(width = 0.5)) + theme_light() + geom_point(position = position_dodge(width = 0.5)) + xlab("Counts")
Run Code Online (Sandbox Code Playgroud)

jar*_*rot 1

我认为没有一种“简单”的方法可以实现这一结果;当您根据所有域的“设置”顺序重新排序 y 轴时max(upper)(尽管我很高兴被证明在这一点上是错误的)。

一种潜在的解决方法可能是按类别分割绘图并分别重新排序,例如

library(tidyverse)

mydat <- data.frame(mean = c(23, 24, 15, 27, 18, 19, 23, 20, 32),
                    lower = c(20, 19, 13, 15, 14, 18, 20, 17, 20),
                    upper = c(25, 29, 17, 39, 22, 20, 26, 23, 40),
                    class = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
                    domain = c("North", "North", "North", "West", "West", "West", "South", "South", "South"))
mydat$class <- as.factor(mydat$class)
mydat$domain <- as.factor(mydat$domain)

colour_palette <- scales::hue_pal()(3)

mydat %>%
  group_split(class) %>%
  map(~{ggplot(data = .x,
               aes(x = mean, y = fct_reorder(.f = class, .x = upper, .fun = max),
                   color = fct_reorder(.f = domain, .x = upper, .fun = max),
                   Group = fct_reorder(.f = domain, .x = upper, .fun = max),
                   xmin = lower, xmax = upper)) +
  geom_errorbar(width = 0.1, position = position_dodge(width = 0.5)) + 
  theme_light() + 
  geom_point(position = position_dodge(width = 0.5)) +
      xlab("Counts") +
      ylab("Class") +
      scale_color_manual(values = c("North" = colour_palette[1],
                                    "South" = colour_palette[2],
                                    "West" = colour_palette[3]),
                         name = "Domain")})
#> [[1]]
Run Code Online (Sandbox Code Playgroud)

#> 
#> [[2]]
Run Code Online (Sandbox Code Playgroud)

#> 
#> [[3]]
Run Code Online (Sandbox Code Playgroud)

创建于 2023 年 10 月 23 日,使用reprex v2.0.2


如果您“手动”执行此操作,则可以按照您想要的顺序排列它们,例如

#> 
#> [[2]]
Run Code Online (Sandbox Code Playgroud)

创建于 2023 年 10 月 23 日,使用reprex v2.0.2