I have following data and code:
> ddf
var1 var2
1 aa 73
2 bb 18
3 cc 9
>
> dput(ddf)
structure(list(var1 = c("aa", "bb", "cc"), var2 = c(73L, 18L,
9L)), .Names = c("var1", "var2"), class = "data.frame", row.names = c(NA,
-3L))
>
>
> ggplot(ddf,aes(x=var1, y=var2, fill=var1))+ geom_bar(width=1, stat='identity')
This creates a barchart with 3 bars. How can I create a single stacked bar from this data. I want to have all these 3 bars stacked on top of each other rather than being separate bars.
This does what you're looking for:
ggplot(ddf, aes(1, var2, fill=var1)) + geom_bar(stat="identity")
You can't specify different x positions and asked them to be stacked at the same time. You have to specify that they're on the same x-position in order for them to be on top of each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With