2

I am having trouble adding text to a grouped barplot in ggplot2. The barplot looks great and the labels added are correct. However, I cannot get the labels to align with corresponding bars...they are all placed in the center of the bar groups. It seems the geom_text(position="dodge") function will not work no matter how many variations I try. Any suggestions would be great.

Here is my data(note that it is a subset):

year   loc group yield
1   2003 volga     0    41
2   2004 volga     0    46
3   2005 volga     0    64
4   2006 volga     0    51
5   2007 volga     0    58
6   2008 volga     0    42
7   2009 volga     0    58
8   2010 volga     0    53
9   2011 volga     0    62
10  2012 volga     0    42
11  2013 volga     0    NA
12  2014 volga     0    NA
61  2003 volga     1    51
62  2004 volga     1    46
63  2005 volga     1    66
64  2006 volga     1    54
65  2007 volga     1    60
66  2008 volga     1    44
67  2009 volga     1    61
68  2010 volga     1    58
69  2011 volga     1    61
70  2012 volga     1    42
71  2013 volga     1    46
72  2014 volga     1    57
121 2003 volga     2    44
122 2004 volga     2    47
123 2005 volga     2    67
124 2006 volga     2    56
125 2007 volga     2    59
126 2008 volga     2    47
127 2009 volga     2    54
128 2010 volga     2    61
129 2011 volga     2    54
130 2012 volga     2    44
131 2013 volga     2    44
132 2014 volga     2    52

And here is my code:

data(mat012)
head(mat012)
#create volga subset
volga=subset(mat012, loc =="volga")
volga
#grouped barplot of volga
ggplot(volga, aes(x=factor(year), y=yield, fill=factor(group))) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=yield), position="dodge")

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Jon K
  • 51
  • 1
  • 5

1 Answers1

2

As mentioned in the answer on the linked question, this should work:

ggplot(volga, aes(x=factor(year), y=yield, fill=factor(group))) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=yield), position = position_dodge(width=1))
Ujjwal
  • 3,088
  • 4
  • 28
  • 36
  • That fixed it. Thank you very much. I tried the position=position_dodge(width=()) command before but I had the width parameter set to 0.9. I guess I didn't realize that changing it to 1 would make a difference...I need to study up on how the width parameter functions a little more. – Jon K Oct 23 '14 at 18:35