I have a data frame which is coded below
Variable=c("ABC","ABC","MNO","MNO","XYZ","XYZ")
Flag=c("Y","N","Y","N","Y","N")
Count=c(1234,9876,6789,3210,5500,7890)
df<-cbind(Variable,Flag,Count)
df<-as.data.frame(df)
I want to plot a bar graph that will show basically the count and the percentage of Y and N for each group.I have done this until now
library(ggplot2)
ggplot(df, aes(x=Variable,fill=Flag))+ geom_bar(position = "dodge")+
stat_count(aes(label=paste0(sprintf("%1.1f", ..count../sum(..count..)*100),
"%n", ..count..), y=1*..count..),
geom="text", colour="white", size=4, position=position_dodge(width=1))
This shows the count For percentage I have tried this
temp <- df %>% group_by( Variable, Flag) %>%
group_by(Variable) %>% mutate(percentage = 100*(Count/sum(Count)))
ggplot(temp, aes(x = Variable, fill = factor(Flag),y=percentage)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = percent(percentage)), vjust = 0.5) +
scale_y_continuous(labels = percent)
I don't now how to bring them together in one plot in R.