2017-02-09 92 views
0

我在本網站上搜索以找到解決方案,但我什麼都沒找到。R - ggplot2:geom_text的位置不起作用

這裏的問題是:

我嘗試修復標籤的位置,在barplot,與geom_text和功能「Y」。但即使我改變「y」的值,位置也不會改變,它會停留在barplot底線的中間。

所以我嘗試使用「vjust」,它可以工作,但是位置是由距離酒吧頂部的距離確定的,所以因爲我的酒吧沒有相同的高度,所以標籤沒有相同的位置。我寧願將它們對齊,而函數「y」也提供這個。

Here is the plot

這裏是我的數據例如:

Phoneme CoG 
s 6112 
s 5946 
s 6545 
s 6097 
s 6245 
s 5604 
s 6189 
s 6030 
s 5386 
s 6105 
s' 5546 
s' 6212 
s' 5963 
s' 5774 
s' 6213 
s' 6118 
s' 5837 
s' 5072 
s' 4642 
s' 4988 

這裏是代碼:

ggplot(data=donnees_M, aes(x=Phoneme, y=CoG, fill=Phoneme)) +  
geom_bar(position=position_dodge(), colour="black", stat="identity") +  
geom_errorbar(aes(ymin=CoG-ci, ymax=CoG+ci), width=.2,position=position_dodge(.9)) +  
labs(x=NULL, y="Centre of Gravity") + theme(panel.background = element_rect(fill = "white") 
, panel.grid.minor=element_line(color = "grey30"), panel.grid.major =element_line(color = "grey30")) + 
scale_fill_manual(values=c("deepskyblue3","seagreen4")) +  
geom_text(aes(y=5.5, label=format(donnees_M$CoG,digits=0)), size = 10) +  
guides(fill=F) +  
theme(axis.title.y = element_text(size = rel(2), angle = 90)) + ylim(0,6000) +  
theme(axis.text.x = element_text(size = rel(3), color="black")) +  
theme(axis.text.y = element_text(size = rel(2.8), color="black")) 

這很奇怪,因爲我已經使用該功能在過去,它的工作沒有任何問題。除了我的數據之外,前面的腳本沒有任何改變。

你對這個問題有什麼想法嗎?

告訴我,如果你需要從我的腳本的詳細信息,我的數據

由於通過提前

+1

一些示例數據將是有益的。我不明白什麼是錯...... – drmariod

+0

我想'x'在'來自'geom_text'的aes'丟失! – ricoderks

+0

感謝您的回覆@drmariod:爲了添加一些數據示例和我的情節圖片,我改變了我的帖子。希望它能幫到更多... – emre

回答

1

實際上y值確實改變了立場,但你在6000的比例使用5.5 ..所以你不能看見。如果你嘗試y=1000,你會看到它。
如果它是一個固定值,你不需要它在AES,也可能是外部的:

geom_text(aes(label=format(donnees_M$CoG,digits=0)), y=1000, size = 10) 

但是,如果你要取決於值的位置,你需要創建一個變量文本的y位置(在這裏看到了吧中部)

donnees_M$text_y <- donnees_M$CoG/2 

ggplot(data=donnees_M, aes(x=Phoneme, y=CoG, fill=Phoneme)) + 
    geom_bar(position=position_dodge(), colour="black", stat="identity") + 
    geom_errorbar(aes(ymin=CoG-ci, ymax=CoG+ci), width=.2,position=position_dodge(.9)) + 
    labs(x=NULL, y="Centre of Gravity") + theme(panel.background = element_rect(fill = "white") 
    , panel.grid.minor=element_line(color = "grey30"), panel.grid.major =element_line(color = "grey30")) + 
    scale_fill_manual(values=c("deepskyblue3","seagreen4")) + 

    geom_text(aes(y=text_y, label=format(donnees_M$CoG,digits=0)), size = 10) + 

    guides(fill=F) + 
    theme(axis.title.y = element_text(size = rel(2), angle = 90)) + ylim(0,6000) + 
    theme(axis.text.x = element_text(size = rel(3), color="black")) + 
    theme(axis.text.y = element_text(size = rel(2.8), color="black")) 
+0

它完美的工作,謝謝!你是偉大的人,我在這個論壇學到很多,再次感謝。 – emre