Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Geom_Bar in GGplot2

Tags:

r

ggplot2

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 stacked bars

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)

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?

Edit: 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')
like image 994
riders994 Avatar asked Sep 20 '26 02:09

riders994


1 Answers

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")
like image 137
mucio Avatar answered Sep 21 '26 16:09

mucio