Using this data:
library(ggplot2)
dd <- data.frame(id = c("A", "A", "B", "B"), prepost = c("pre", "post"),
value = 1:4)
this one works:
qplot(id, value, data = dd, fill = prepost, geom = "bar")
however, the next one gives the indicated error message. The only difference between the two is the addition of group = prepost to the end of the command; however, since we had already written fill = prepost that should be the default group anyways.
> qplot(id, value, data = dd, fill = prepost, geom = "bar", group = prepost)
Error in pmin(y, 0) : object 'y' not found
We can fix up the last one by adding stat = "identity" like this:
qplot(id, value, data = dd, fill = prepost, geom = "bar", group = prepost,
stat = "identity")
I have two questions:
(a) Why does the qplot which gave the error message not work when the others do work?
(b) If we use a continuous y aesthetic with geom_bar then what is supposed to happen if one does not specify stat? From the first qplot it seems that in that case it acts as if stat="identity" but in the presence of group specifying stat="identity" or not reveals a difference.
(By the way, this question seems somewhat related although its different enough that it does not seem to answer this question: Issue with ggplot2, geom_bar, and position="dodge": stacked has correct y values, dodged does not)
A good question!
I will echo a earlier comment by @joran
Your
qplotcall can be recreated usingNow, if you read the help for
geom_barit lists the aesthetics it understands,groupis not one of them (so perhaps you can’t expect it to work as you wish when you do this)If you read the help for
groupyou will see that it is an aesthetic that allows you to override the default interaction of all discrete variables.If you group by
prepostalone, you are not grouping by the discretexaxis variableid, which the default would be included as well,Therefore
works, but the grouping is entirely redundant as this is the default.
If you specify just
prepostoridas your grouping, it confusesstat_bin(the underlying method that is crunching the numbers to create the values for the bar plot. Hence you need to usestat_identityinstead.EDIT: as noted by the OP in the comments below, this is related to a known issue and will give better warnings in the next version (or th current dev version on github
From the NEWS