R ggplot在错误栏上放置标签

时间:2014-02-26 21:00:04

标签: r ggplot2

我可以使用错误栏创建条形图但是如何在较高的{vLABELH)和较低的错误栏(vLABELL)上添加标签。

library(ggplot2)
vx <- c(1:5)
vBAR <- c(0.1,0.2,0.3,0.4,0.5)
vLINE1 <- c(0.15,0.25,0.35,0.45,0.55)
vLINE2 <- c(0.15,0.25,0.35,0.45,0.55)
vLINE3 <- c(0.05,0.15,0.25,0.35,0.45)
vLABELL<- c(0.05,0.15,0.25,0.35,0.45)
vLABELH <- c(0.15,0.25,0.35,0.45,0.55)
df1 <- as.data.frame(cbind(vx,vBAR,vLINE1,vLINE2,vLINE3,vLABELL,vLABELH))
class(df1)
barchart1  <- ggplot(df1, aes(x=as.factor(vx),y=vBAR)) + geom_bar(fill="blue",     colour="blue")+ 
geom_errorbar(aes(ymin=vLINE3, ymax=vLINE1 ))
barchart1

1 个答案:

答案 0 :(得分:3)

我想你正在寻找geom_text

ggplot(df1, aes(x = as.factor(vx), y = vBAR)) + 
  geom_bar(stat = "identity", fill = "blue", colour = "blue") + 
  geom_errorbar(aes(ymin = vLINE3, ymax = vLINE1 )) +
  geom_text(aes(label = vLABELH, y = vLINE1), vjust = -.5) +
  geom_text(aes(label = vLABELL, y = vLINE3), vjust = 1.5) 

enter image description here