-1

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")
Roze
  • 23
  • 1
  • 7
  • 1
    Could you provide actual data rather than an image? Images do not help SO users to give you a hand since they need to type your data from the beginning. Have a look of [**this post**](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – jazzurro Feb 19 '18 at 02:10
  • Thanks for your comment, I added more details. – Roze Feb 19 '18 at 03:49

1 Answers1

1

It looks like geom_bar will only plot bars for observations that exist; if you want to have bars for every method (assuming you want each level to have a bar for each method), you need to have observations in your data corresponding to those pairings. Currently, it looks like each level corresponds to two methods at most. To artificially generate those pairings, you can use tidyr::complete() and tidyr::expand() before plotting. For each new pairing, ave will automatically be assigned NA, but you can change this behavior using the fill parameter in tidyr::complete().

Here's an example where ave is set to 0 for every new pairing instead of NA:

dat %>%
  complete(expand(dat, levels, method), fill = list(ave = 0)) %>%
  ggplot(df4,mapping = aes(x = as.factor(levels),
                           y = ave, 
                           fill = as.factor(method),
                           )) + 
  geom_bar(stat = "identity", position = position_dodge(width = 1))+ 
  xlab("levels") + 
  ylab("Mean")
anant
  • 448
  • 3
  • 8
  • Thanks for your comment @anant, I tried your solution but it does not work. Meanwhile, I provided more details to my question. Would you please look at it and let me know if you have any solution. Thanks in advance. – Roze Feb 19 '18 at 04:01
  • @Roze I think I see the issue now that the full dataframe is up-- it looks like you have overlapping datapoints. For instance, 'D' is associated with 'G3' twice, once with ave = 2 and once with ave = 5. That's why on the plot, you only see the bar associated with the higher value (5); it covers the other value. If you add `aes(alpha = 0.5)` you'll see what I mean. `geom_bar` might not be the most suitable visualization for your problem. – anant Feb 19 '18 at 04:09
  • [Here's a version of the plot illustrating the problem.](https://i.imgur.com/aDiXflA.png) – anant Feb 19 '18 at 04:16
  • Thank you very much. I will figure it out. – Roze Feb 19 '18 at 04:16