在GGplot2中使用Geom_Bar [英] Using Geom_Bar in GGplot2
问题描述
因此,我试图在ggplot2中使用geom_bar,我看到人们在网上展示它的所有情况都是某些事物的比较频率.我想要做的图表就是这样的堆积条形图
So I'm trying to use geom_bar in ggplot2, and all of the cases that I see of people demonstrating it online are of comparative frequencies of certain things. The chart that I'm trying to do is the stacked bar graph like this one
但是,我想从值向量中做到这一点.也就是说,假设我有向量
However, I want to do it from a vector of values. That is, let's say I have the vector
v=c(1,2,3,4)
我想得到的不是4个偶数小节,而是一堆4个小节,其中最上面的是1个单位高,而下一个是2个单位(依此类推).在R中可以吗?
Instead of 4 even bars, which is what I understand I would get, I'd like a stack of 4 bars where the top one is 1 unit tall, and the next one down is 2 units tall (etc.). Is this possible in R?
这是我用于图形的代码.它会生成一个普通的条形图,而不是我正在寻找的堆叠版本:
Here is the code that I've used for my graph. It's yielding a normal bar graph, not the stacked version that I'm looking for:
ggplot(data = v, aes(x = factor(x), y = y)) + geom_bar(aes(fill = factor(y)),stat = 'identity')
推荐答案
我认为您可以从这里开始:
I think you can start from this:
v=data.frame(x="My Stacked Bar", y=c(1,2,3,4))
ggplot(data = v, aes(x = factor(x), y = y))+
geom_bar(aes(fill=factor(y)), stat="identity")
这篇关于在GGplot2中使用Geom_Bar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!