Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single bar barchart in ggplot2, R

Tags:

r

ggplot2

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.

like image 632
rnso Avatar asked Sep 10 '14 17:09

rnso


1 Answers

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.

like image 114
Señor O Avatar answered Sep 19 '22 00:09

Señor O