2014-02-26 55 views
0

我可以創建帶有錯誤條的條形圖,但是如何將標籤放在較高(vLABELH)和較低錯誤條(vLABELL)上。R ggplot在錯誤條上放置標籤

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 
+0

來吧,你肯定不是在SO一個新手。快速[在SO **上搜索](http://stackoverflow.com/search?q= [ggplot] +標籤+酒吧)絕對會讓你進入'geom_text'。請展示一些努力! – Henrik

回答

2

我想你正在尋找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

+0

非常感謝。這非常有幫助。 –