I have following dataframe:
col1 col2 col3 col4
Y N N N
Y N Y Y
Y N N Y
Y N N N
I would like to use ggplot to plot a chart with %values written in bar itself.
I have following dataframe:
col1 col2 col3 col4
Y N N N
Y N Y Y
Y N N Y
Y N N N
I would like to use ggplot to plot a chart with %values written in bar itself.
This should do just fine
#your data.frame
df <- read.table(text=
"col1 col2 col3 col4
Y N N N
Y N Y Y
Y N N Y
Y N N N", header=T)
library(reshape2)
df$ID <- 1:length(df)
df <- melt(df, id.vars = "ID") #melting the data.frame into long format
library(ggplot2) #and ploting it
ggplot(df)+
geom_bar(aes(x=variable, fill=value), stat="count")
For an outcome in percentages you can do
ggplot(df) +
geom_bar(aes(x=variable, y= (..count..)/sum(..count..), fill=value))