I want to have a barplot using ggplot2 that display multiple bars within each group, but in my plot, I have 4 bars instead of 8 for each group. I will appreciate your help. here is my code:
levels = c('D', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9')
method = c('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7','G8')
ave = c(4, 4, 4, 4, 5, 1, 2, 6, 3, 5, 2, 2, 2, 2, 5, 3, 4, 1, 1, 1, 2,
2, 2, 2, 3, 3, 2, 1, 1, 1, 1, 3, 4, 5, 6, 8, 9, 7, 1, 2, 3, 3, 4, 5, 7,
6, 1, 1, 1, 2, 5, 7, 7, 8, 9, 1, 4, 6, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
levels = factor(c(rep(levels,8)))
method = factor(c(rep(method,10)))
dat = data.frame(levels,ave,method)
dodge = position_dodge(width = .9)
p = ggplot(dat,mapping =aes(x = as.factor(levels),y = ave,fill =
as.factor(method)))
p + geom_bar(stat = "identity",position = "dodge") +
xlab("levels") + ylab("Mean")