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

使用geom_errorbarh出现垂直误差棒,如何设置为水平误差棒?

问题:ggplot2绘制水平误差棒失败,显示为垂直误差棒

我使用以下R语言数据集与ggplot2代码绘制图表,预期生成水平误差棒,但实际得到垂直误差棒,需要调整代码实现水平误差棒可视化。

数据集
discrimination <- structure(list(Mean = c(0.617364075449035, 0.57284608439026, 
                        0.496959052786028, 0.460522529509851, -0.0824568013522042, -0.509293992266701),
               variable = c("A", "B", "C", "D", "E", "F"),  
cilow = c(0.615581317704699, 0.571019568616708, 0.495348349475911, 0.458631736688209, -0.0842685444311714, -0.511175368198272), 
cihigh = c(0.619146833193371, 0.574672600163811, 0.498569756096144, 0.462413322331492, -0.080645058273237, -0.5074126163351),rank = 1:6),
row.names = c("A", "B", 
              "C", "D", "E", 
              "F"), class = "data.frame")
原绘图代码
discrimination_plot <- ggplot(discrimination, aes(y = rank, x = Mean)) +
  geom_point(shape = 18, size = 1) +  
  geom_errorbarh(aes(xmin = cilow, xmax = cihigh), height = 0.25) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
  scale_y_continuous(name = "", breaks=1:6, labels = discrimination$variable, trans = "reverse") +
  xlab("Parameter") + 
  ylab(" ") + 
  theme_classic() +
  theme(panel.border = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 12, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black"))+ggtitle("XXX")

discrimination_plot
当前生成的图表

当前生成的图表

解决方案

你的代码里已经用了geom_errorbarh(水平误差棒专用函数),但显示异常的原因是**scale_y_continuous中的trans = "reverse"参数**——这个反转y轴的操作会干扰geom_errorbarh的方向渲染。

下面提供两种修正方法,都能正常显示水平误差棒,同时保持y轴变量的倒序排列:

方法1:调整rank顺序替代y轴反转

# 反转rank值,实现y轴倒序显示
discrimination$rank <- rev(1:6)

discrimination_plot <- ggplot(discrimination, aes(y = rank, x = Mean)) +
  geom_point(shape = 18, size = 1) +  
  geom_errorbarh(aes(xmin = cilow, xmax = cihigh), height = 0.25) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
  scale_y_continuous(name = "", breaks=1:6, labels = rev(discrimination$variable)) +
  xlab("参数值") + 
  ylab(" ") + 
  theme_classic() +
  theme(panel.border = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 12, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black")) +
  ggtitle("XXX")

discrimination_plot

方法2:直接将variable设为有序因子映射到y轴

# 指定variable的显示顺序
discrimination$variable <- factor(discrimination$variable, levels = c("F", "E", "D", "C", "B", "A"))

discrimination_plot <- ggplot(discrimination, aes(y = variable, x = Mean)) +
  geom_point(shape = 18, size = 1) +  
  geom_errorbarh(aes(xmin = cilow, xmax = cihigh), height = 0.25) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
  xlab("参数值") + 
  ylab(" ") + 
  theme_classic() +
  theme(panel.border = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 12, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black")) +
  ggtitle("XXX")

discrimination_plot

内容的提问来源于Stack Exchange,提问作者Nader Mehri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 05:31:20