0

I have now tried to fix manually create error bars into my barplot? Is that possible and if yes, how do I do that? Furthermore, is it possible and how do I add text between to plots for showing significance levels? (such as Barplot with significant differences and interactions?)

I would appreciate any kind of help!

My code look like this:

pdf(file="barplo1.pdf",  title="WHAT TITLE HERE?", pointsize=6, width=4, height=4, paper="special")
par(mfrow = c(3, 2), oma=c(0.5,0.5,0.5,0.5), mar=c(2,5,2,2))
barplot(c(36.5,49.8), names.arg = c("Low", "High"), beside = TRUE, ylab = "", main="Power", col = c("grey", "gray50"), ylim = c(0,100), cex.lab=1.7, cex.axis = 1.4, cex.names = 1.4, cex.main = 1.8, lwd = 0.5, border = NA)
text(0.71,39.5, "36,5 pct.***", pos=3, cex= 1.4)
text(1.9,52.8, "49,8 pct.***", pos=3, cex= 1.4)
barplot(c(39.5,50.6), names.arg = c("Low", "High"), beside = TRUE, main="", col = c("grey", "gray50"), ylim = c(0,100), cex.axis = 1.4, cex.names = 1.4, cex.main = 1.8, lwd = 0.5, border = NA)
text(0.71,42.5, "39,5 pct.***", pos=3, cex= 1.4)
text(1.9,53.6, "50,6 pct.***", pos=3, cex= 1.4)
barplot(c(33.5,50.1), names.arg = c("Low", "High"), beside = TRUE, ylab = "", col = c("grey", "gray50"), ylim = c(0,100), cex.lab=1.7, cex.axis = 1.4, cex.names = 1.4, lwd = 0.5, border = NA)
text(0.71,36.5, "33,5 pct.***", pos=3, cex= 1.4)
text(1.9,53.1, "50,1 pct.***", pos=3, cex= 1.4)
barplot(c(49.5,61.5), names.arg = c("Low", "High"), beside = TRUE, col = c("grey", "gray50"), ylim = c(0,100), cex.axis = 1.4, cex.names = 1.4, lwd = 0.5, border = NA)
text(0.71,52.5, "49,5 pct.***", pos=3, cex= 1.4)
text(1.9,64.5, "61,5 pct.***", pos=3, cex= 1.4)
barplot(c(40.0,48.5), names.arg = c("Low", "High"), beside = TRUE,  col = c("grey", "gray50"), ylim = c(0,100), ylab = "", cex.lab=1.7, cex.axis = 1.4, cex.names = 1.4, lwd = 0.5, border = NA)
text(0.71,43.0, "40,0 pct.***", pos=3, cex= 1.4)
text(1.9,51.5, "48,5 pct.***", pos=3, cex= 1.4)
barplot(c(45.5,70.2), names.arg = c("Low", "High"), beside = TRUE, col = c("grey", "gray50"), ylim = c(0,100), cex.axis = 1.4, cex.names = 1.4, lwd = 0.5, border = NA)
text(0.71,48.5, "45,5 pct.***", pos=3, cex= 1.4)
text(1.9,73.2, "70,2 pct.***", pos=3, cex= 1.4)
dev.off()
Community
  • 1
  • 1
A. F.
  • 101

1 Answers1

0

To do this, you'll need to run each call to barplot() twice, once to get a vector with the x coordinates at the midpoint of each bar to be drawn and then a second time to make the plot. Then, after making the plot, you can use segments() to add a line representing the CI in the middle of each bar. Here's a simple example:

x <- c(4, 3, 7)
z <- barplot(x, plot=FALSE)  # This returns a vector with the x-axis midpoints of the bars it would have drawn; they are *not* integers, which is why this is tricky
png("barplot.with.cis.png", width=5, height=5, unit="in", res=150)
par(mai=c(0.5,0.5,0.1,0.1))
barplot(x)
segments(x0=z, x1=z, y0=x-1, y1=x+1)
dev.off()

That produces this plot:

enter image description here

In your case, you'd want to plug the lower and upper bounds of your CIs in as your y0 and y1 vectors in your call to segments(). You can also use that z vector as the x coordinates for your text annotations; the corresponding values will be your y coordinates.

Finally, notice that you need to set the ylim in the call to barplot(x) to leave room for the CIs to come. You could also key that off of your CI's upper bounds with something like ylim=c(0, ceiling(max([vector of CI upper bounds]))).

ulfelder
  • 5,305
  • 1
  • 22
  • 40