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")
我想通过减小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")
我认为没有一种“简单”的方法可以实现这一结果;
max(upper)
当您根据所有域的“设置”顺序重新排序 y 轴时(尽管我很高兴被证明在这一点上是错误的)。一种潜在的解决方法可能是按类别分割绘图并分别重新排序,例如
创建于 2023 年 10 月 23 日,使用reprex v2.0.2
如果您“手动”执行此操作,则可以按照您想要的顺序排列它们,例如
创建于 2023 年 10 月 23 日,使用reprex v2.0.2
我无法计算出与您期望的相同的输出,但希望以下代码对您同样有意义(其中
domain
信息作为标签添加到每个错误栏)数据