2017-02-09 96 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