'What is the difference between "aes(..., stat=count)" and "aes(...), stat="count"
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!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
