You can use geom_segment()
to add those lines. Use boxplot.nr-0.5
for start of lines and boxplot.nr+0.5
for the end of lines and value
for y and yend. Also add inherit.aes=FALSE
inside geom_segment()
to ensure that geom_segment()
doesn't look for the variable X1
that you use for the fill for boxplot.
ggplot(abc, aes(x = X1, ymin = `0%`, lower = `25%`, middle = `50%`,
upper = `75%`, ymax = `100%`)) +
geom_boxplot(aes(fill = X1), stat = "identity")+
geom_segment(data=somelines,aes(x=boxplot.nr-0.5,xend=boxplot.nr+0.5,
y=value,yend=value),inherit.aes=FALSE,color="orange",size=1.5)
The same result can be atchieved also with second call to geom_boxplot()
- as there is only one value in each level for somelines object, then boxplots will appear as lines.
ggplot(abc, aes(x = X1, ymin = `0%`, lower = `25%`, middle = `50%`,
upper = `75%`, ymax = `100%`)) +
geom_boxplot(aes(fill = X1), stat = "identity")+
geom_boxplot(data=somelines,aes(factor(boxplot.nr),value),
inherit.aes=FALSE,color="orange",size=1.5)