Reputation: 2825
I have a data frame example
with two variables V1 and V2, both are dummy variables. I want to create a stacked proportional graph with V1 as x axis.
I tried the following, but the graph is not showing up:
library(ggplot2)
library(plyr)
library(dplyr)
example<-as.data.frame(cbind(c(0,0,0,0,1,1,1,0,1),c(0,1,0,0,1,0,0,0,1)))
class(example$V1)
class(example$V2)
ce = ddply(example, "V1", mutate, percent_v2 = sum(V2)/length(V2) * 100)
ggplot(ce, aes(x=V1, y=percent_v2, fill=V2),geom_bar(stat='identity'))
I thought maybe fill=V2
is wrong because both V1 and V2 are integers, so I tried as.character(V2)
instead, but this did not work as well.
Upvotes: 1
Views: 66
Reputation: 1795
Try:
ggplot(ce, aes(x=V1, y=percent_v2, fill=V2))+geom_bar(stat="identity")
V1 V2 percent_v2
1 0 0 20
2 0 1 20
3 0 0 20
4 0 0 20
5 0 0 20
6 1 1 50
7 1 0 50
8 1 0 50
9 1 1 50
Upvotes: 1