I have a data-set as below:
A B C
1 1 1
0 1 1
1 0 1
1 0 1
I want to have a stack bar chart that shows percentage of 1 and 0 in each column next to other column all in one figure.
I have a data-set as below:
A B C
1 1 1
0 1 1
1 0 1
1 0 1
I want to have a stack bar chart that shows percentage of 1 and 0 in each column next to other column all in one figure.
There are several steps you need to take:
tidyr::gather
)ggplot
's geom_bar
First you need to tidy your data
library(tidyr)
A = c(1,0,1,1)
B = c(1,1,0,0)
C = c(1,1,1,1)
data = data.frame(A,B,C)
data = gather(data, key = type, value = val)
Then compute your statistics
library(dplyr)
perc = group_by(data, type) %>% summarise(perc = sum(val)/length(val))
To finish plot them
library(ggplot2)
ggplot(perc) + aes(x = type, y = perc) + geom_bar(stat = "identity")