I am doing a plot similar to this idea
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
I got the next plot:
However, I would like to change each border of each segment where I have added a red point a line.
One quick option would be to add a
geom_errorbar
before thegeom_segment
. One drawback is that the segment will overlap the errorbars. Hence, IMHO the cleanest approach would be use twogeom_segments
to add the "errorbar" like line endings. To this end we have to convert yourname
column to a numeric after (!!) filtering the dataset:And as a reference here is the code using
geom_errobar
: