ggplot2,R中的单条酒吧 [英] Single bar barchart in ggplot2, R

查看:130
本文介绍了ggplot2,R中的单条酒吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据和代码:

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')

这会创建一个3条形的条形图。我怎样才能从这些数据创建一个单一的堆栈栏。我希望所有这3个酒吧堆叠在一起,而不是单独的酒吧。

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")

您不能指定不同的x位置,并要求它们同时堆叠。您必须指定它们位于同一个x位置,以便它们彼此重叠。

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.

这篇关于ggplot2,R中的单条酒吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆