Single bar barchart in ggplot2, R

2019-06-21 08:21发布

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.

标签: r ggplot2
1条回答
Luminary・发光体
2楼-- · 2019-06-21 08:42

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.

查看更多
登录 后发表回答