如何在barplot中手动输入错误行?

时间:2015-08-13 17:24:25

标签: r text plot error-handling bar-chart

我现在尝试修复手动创建错误条到我的条形图中?这是可能的,如果是的话,我该怎么做?此外,它是否可能,如何在绘图之间添加文本以显示显着性水平? (例如Barplot with significant differences and interactions?

我会感激任何帮助!

我的代码如下:

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()

1 个答案:

答案 0 :(得分:0)

要做到这一点,你需要每次调用barplot()两次,一次得到一个矢量,其中x坐标位于每个条形图的中点,然后第二次制作图形。然后,在绘制图表后,您可以使用segments()在每个条形图的中间添加代表CI的线条。这是一个简单的例子:

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()

产生这个情节:

enter image description here

在您的情况下,您希望在调用segments()时将CI的下限和上限作为y0和y1向量插入。您还可以使用z向量作为文本注释的x坐标;相应的值将是你的y坐标。

最后,请注意您需要在ylim的调用中设置barplot(x),以便为CI留出空间。您还可以使用类似ylim=c(0, ceiling(max([vector of CI upper bounds])))的内容来关闭CI的上限。