emre
emre

Reputation: 3

R - ggplot2 : the position for the geom_text doesn't work

i searched on this website to find a solution, but i've found nothing.

Here is the problem :

I try to fix the position of labels in a barplot, with geom_text and the function "y". But even if i change the value of "y", the position doesn't change, it stays at the middle of the bottom line of the barplot.

So i tried with "vjust", it works but the position is determined from the distance with the top of the bars, so since my bars don't have the same height, the labels don't have the same position. I would prefer to align them, and the function "y" provides that.

Here is the plot

Here is my data example :

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

here is the code :

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

It's strange because i already used that function in the past and it worked without any problem. And nothing has changed from the previous script, except my datas.

Do you have any idea of the problem?

Tell me if you need more informations from my script and my data

Thanks by advance

Upvotes: 0

Views: 1516

Answers (1)

timat
timat

Reputation: 1500

actually y values does change the position, but you use 5.5 in a scale of 6000.. so you cannot see it. If you try y=1000 you would see it.
If it is a fix value, you do not need it in the aes, it could be outside:

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

But if you want the position to depend of the value, you need to create a variable with the y position of the text: (see here for middle of the bar)

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

Upvotes: 2

Related Questions