Reputation: 27
take this dataset:
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
#There is no difference between this:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, group = 1), stat = "count")
#and this:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(count), group = 1))
However, if you replace "count" with "prop", only the later command works, the former returning an error. Why is this? why does it work for count and not prop?
Any help would be greatly appreciated!
Upvotes: 0
Views: 359
Reputation: 41260
count
and prop
are two variables computed by geom_bar
when using stat="count"
, and can be reached by after_stat()
, stat()
or ..count..
/..prop..
:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = after_stat(count), group = 1), stat= "count")+
geom_text(mapping = aes(x = cut, y = stat(count),label=round(..prop..,2),vjust=-1, group = 1),stat="count")
The other option is to use
stat="identity"
, in this case prop
and count
aren't calculated because values are left as is, without counting.
There's not stat="prop"
, hence the error message you get.
Upvotes: 1