I’ve discovered what I think is a bug in ggplot2’s use of geom_text and am wondering if anyone out there has a work-around/is able to point out what I’m doing wrong.
Consider the following sample code:
library(ggplot2)
Treatment <- rep(c('C','T'),3)
Group <- rep(c('A','B','C'),each=2)
Response <- c(0.22894321, 0.23391813, 0.92256514, 0.92705167, 0.05982670, 0.09667674)
Differences <- c('+0pts', '+0pts', '+0pts', '+0pts', '+4pts', '+4pts')
df <- data.frame(Treatment, Group, Response, Differences)
ydef <- c(0.03, 0.03, 0.2)
hist <- ggplot(df, aes(x=Group, y=Response, fill=Treatment, stat="identity"))
hist + geom_bar(position = "dodge") + geom_text(aes(label = Differences,y=ydef))
If this works for you the same way that it does for me–using the most current versions of ggplot2 and R 2.12.2–groups B and C should have two text floats each, spaced equal distances apart. Obviously this is not what we want. Now change ‘ydef’ such that
ydef <- c(0.03, 0.03, 0.03)
re-run the script above, and the problem is solved–each group now only has one text float corresponding to differences (or rather two, layered exactly on top of each other). How does this make any sense? I’ve been using this basic structure in a fairly large scale application, and have not had any trouble until I got to this specific example with these specific numbers.
Part of the problem may be the way I’m constructing ‘Differences’, by adding it as a column to the data frame and placing this text on the chart with geom_text. If there was a way to do the same thing using a vector with only three character entries I’m sure that would solve the problem.
In short (1) help please! and (2) here is a potentially interesting bug for the consideration of the ggplot2 community.
Cheers,
Aaron
I don’t think this is a bug, I think you’re not using aes() properly. The aesthetics set in aes() ought to refer to a variable in a data frame. Your data frame (for which some here may chide you for naming ‘df’) doesn’t have a variable named ydef in it. If you add it to your data frame, it works just fine for me:
Note that the values need to be repeated in accordance with the levels of Group.
If you’re going to set an aesthetic to a single value not contained in a data frame passed to the geom, it should be just that, a single value, and it should happen outside the aes() call.
EDIT: To explain the behavior you’re seeing, you’re passing a vector of length 3 to ggplot, which then tries to reconcile it with a data frame with 6 rows. So R recycles the vector, leading to this situation:
So now you have two levels with two distinct values associated with them.