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

如何在ggplot2中为垂直线段端点添加横向短划线?

问题

我正在使用ggplot2和ggrepel包绘制图表,当前代码如下:

library(ggrepel)
library(ggplot2)

name<-c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15","A16","A17","A18")

gr<-factor(c("A","A","A","A","B","B","B","B","C","C","C","C","D","D","D","D","D","D"))
est<-c(6.5,-5.7,7.1,7.5,7.1,6.6,11.0,10.8,-13.9,-8.1,10.1,8.2,-11.1,-5.7,-7.7,-6.5,-4.8,-5.5)
low<-c(1.5,-10.7,2.4,2.5,1.4,0.9,2,3.6,-22.9,-16,0.1,1,-19.6,-10.1,-14.4,-12.7,-9.3,-10.9)
up<-c(11.6,-0.7,11.8,12.4,12.7,12.3,20.1,18.1,-4.9,-0.1,20.1,15.4,-2.6,-1.3,-1.1,-0.2,-0.3,-0.2)

df1<-data.frame(name,gr,est,low,up)

#Plot 1

p1<-ggplot(df1%>%filter(gr=="A"), aes(x=name, est,color="blue2"))+theme(legend.position = "none")+
  scale_color_manual(values = colores[c(7,3)])+
  geom_segment(aes(x=name,xend=name,y=up,yend=low),size=1.2)+
  geom_point(aes(x=name,y=est,color="red"))+
  geom_point(aes(x=name,y=low,color="red"))+
  geom_point(aes(x=name,y=up,color="red"))+
  theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
  scale_fill_manual(values = colores)+
  geom_label_repel(aes(label = est,y = est),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   segment.color = 'grey50') +
  
  geom_label_repel(aes(label = low,y = low),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   segment.color = 'grey50') +
  
  geom_label_repel(aes(label = up,y = up),label.size = 0.5,
                   box.padding   = 0.25, 
                   point.padding = 0.9,
                   segment.color = 'grey50') +labs(title="Title",
                                                   subtitle=" Subtitle")+coord_flip()
p1

当前已生成带有垂直线段、端点红点及标签的图表,但希望为每个线段的端点(红点位置)添加横向短划线,寻求代码修改方案。

解决方案

要给线段端点添加横向短划线,可以使用geom_segment分别绘制low和up位置的水平短线。由于代码中使用了coord_flip(),轴的方向会翻转,需要注意线段的坐标计算逻辑。

修改后的完整代码如下:

library(ggrepel)
library(ggplot2)
library(dplyr) # 加载dplyr以支持filter函数

name<-c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15","A16","A17","A18")

gr<-factor(c("A","A","A","A","B","B","B","B","C","C","C","C","D","D","D","D","D","D"))
est<-c(6.5,-5.7,7.1,7.5,7.1,6.6,11.0,10.8,-13.9,-8.1,10.1,8.2,-11.1,-5.7,-7.7,-6.5,-4.8,-5.5)
low<-c(1.5,-10.7,2.4,2.5,1.4,0.9,2,3.6,-22.9,-16,0.1,1,-19.6,-10.1,-14.4,-12.7,-9.3,-10.9)
up<-c(11.6,-0.7,11.8,12.4,12.7,12.3,20.1,18.1,-4.9,-0.1,20.1,15.4,-2.6,-1.3,-1.1,-0.2,-0.3,-0.2)

df1<-data.frame(name,gr,est,low,up)

# 定义横向短划线的长度(可根据图表大小调整)
dash_length <- 0.15

p1<-ggplot(df1%>%filter(gr=="A"), aes(x=name, y=est))+
  # 绘制主垂直线段
  geom_segment(aes(x=name,xend=name,y=up,yend=low),size=1.2, color="blue2")+
  # 绘制low端点的横向短划线
  geom_segment(aes(x=as.numeric(name)-dash_length, xend=as.numeric(name)+dash_length, y=low, yend=low), color="red")+
  # 绘制up端点的横向短划线
  geom_segment(aes(x=as.numeric(name)-dash_length, xend=as.numeric(name)+dash_length, y=up, yend=up), color="red")+
  # 绘制三个位置的红点
  geom_point(aes(y=est), color="red")+
  geom_point(aes(y=low), color="red")+
  geom_point(aes(y=up), color="red")+
  # 添加标签
  geom_label_repel(aes(label = est,y = est),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   segment.color = 'grey50') +
  geom_label_repel(aes(label = low,y = low),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   segment.color = 'grey50') +
  geom_label_repel(aes(label = up,y = up),label.size = 0.5,
                   box.padding   = 0.25, 
                   point.padding = 0.9,
                   segment.color = 'grey50') +
  # 设置图表标题与主题
  labs(title="Title", subtitle="Subtitle")+
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
  coord_flip()

p1

关键修改说明:

  • 由于coord_flip()翻转了轴,分类变量name会被转为数值型,通过as.numeric(name)计算短划线的左右偏移,dash_length控制短划线的长度,可按需调整。
  • 直接指定颜色值(如color="blue2"),避免原代码中未定义的colores变量导致报错,同时简化颜色映射逻辑。
  • 补充加载dplyr包,确保filter函数正常运行。

内容的提问来源于stack exchange,提问作者user3483060

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 14:01:14