0

I've been struggling with how to make a bar plot for the following table. I want to show two bars for input and nonchimeric numbers side by side and fill them in based on the treatment. And I need each bar labelled according to the samplename. I have already tried the melt method in R, but I didn't get the results I want. Any suggestions will be helpful! Thank you!

samplename         treatment input nonchimeric
NegCtrlr1  negcontrolr1  Negative control  3659        1493
NegCtrlr2  negcontrolr2  Negative control 37085       28643
NegCtrlr3  negcontrolr3  Negative control 10039        6109
S1r1               OAr1 Before incubation 72536       47228
S1r2               OAr2 Before incubation 69128       48585
S1r3               OAr3 Before incubation 73710       51423
S2r1               OBr1 Before incubation 64491       47085
S2r2               OBr2 Before incubation 69254       51022
S2r3               OBr3 Before incubation 67584       50864
S3r1               SCr1  After incubation 37491       24150
S3r2               SCr2  After incubation    23           0
S3r3               SCr3  After incubation 81075       52826
S4r1               SAr1  After incubation 64943       46154
S4r2               SAr2  After incubation 64458       47047
S4r3               SAr3  After incubation 69098       52849
S5r1               SBr1  After incubation 28408       20997
S5r2               SBr2  After incubation    22           4
S5r3               SBr3  After incubation 57438       41525
S6Ar1              OCr1 Before incubation 73730       62730
S6Ar2              OCr2 Before incubation 60497       50070
S6Ar3              OCr3 Before incubation 56797       49685
ZymoMockr1 poscontrolr1  Positive control 65887       21350
ZymoMockr2 poscontrolr2  Positive control 14716        3976
ZymoMockr3 poscontrolr3  Positive control 73301       22709

As suggested, I tried to this table reproducible using dput() below.

reads_data_struc <-
  structure(
    list(
      samplename = c(
        "negcontrolr1",
        "negcontrolr2",
        "negcontrolr3",
        "OAr1",
        "OAr2",
        "OAr3",
        "OBr1",
        "OBr2",
        "OBr3",
        "SCr1",
        "SCr2",
        "SCr3",
        "SAr1",
        "SAr2",
        "SAr3",
        "SBr1",
        "SBr2",
        "SBr3",
        "OCr1",
        "OCr2",
        "OCr3",
        "poscontrolr1",
        "poscontrolr2",
        "poscontrolr3"
      ),
      treatment = c(
        "Negative control",
        "Negative control",
        "Negative control",
        "Before incubation",
        "Before incubation",
        "Before incubation",
        "Before incubation",
        "Before incubation",
        "Before incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "After incubation",
        "Before incubation",
        "Before incubation",
        "Before incubation",
        "Positive control",
        "Positive control",
        "Positive control"
      ),
      input = c(
        3659,
        37085,
        10039,
        72536,
        69128,
        73710,
        64491,
        69254,
        67584,
        37491,
        23,
        81075,
        64943,
        64458,
        69098,
        28408,
        22,
        57438,
        73730,
        60497,
        56797,
        65887,
        14716,
        73301
      ),
      nonchimeric = c(
        1493,
        28643,
        6109,
        47228,
        48585,
        51423,
        47085,
        51022,
        50864,
        24150,
        0,
        52826,
        46154,
        47047,
        52849,
        20997,
        4,
        41525,
        62730,
        50070,
        49685,
        21350,
        3976,
        22709
      )
    ),
    class = "data.frame",
    row.names = c(
      "NegCtrlr1",
      "NegCtrlr2",
      "NegCtrlr3",
      "S1r1",
      "S1r2",
      "S1r3",
      "S2r1",
      "S2r2",
      "S2r3",
      "S3r1",
      "S3r2",
      "S3r3",
      "S4r1",
      "S4r2",
      "S4r3",
      "S5r1",
      "S5r2", "S5r3", "S6Ar1", "S6Ar2", "S6Ar3", "ZymoMockr1", "ZymoMockr2", 
                                                                                                                                                                                                                                                                   "ZymoMockr3"))
library(ggplot2)

reads_data_melt <- melt(reads_data_struc, id.vars = 'samplename', variable.name = 'treatment')
rownames(reads_data_melt)
colnames(reads_data_melt)
ggplot(reads_data_melt, aes(reads_data_melt$samplename, reads_data_melt$value, fill=treatment )) + geom_col(position = "dodge") 


And plot it produces is: Processed reads count

tslb14
  • 11
  • 3
  • What does your current code look like? How is your current result different that the results you want? It's easier to start from code you already tried that have to start over from scratch. – MrFlick Jul 21 '21 at 18:13
  • True. Sorry about that. My current code is posted now. Thank you. – tslb14 Jul 21 '21 at 21:15
  • Would you mind sharing your data as a `dput()`? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). To post your data type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Jul 22 '21 at 05:37
  • Hi @stefan - thank you for teaching me about `dput()`. I hope I made the table reproducible correctly. – tslb14 Jul 22 '21 at 17:41

1 Answers1

0

You need to pivot the table into "long" format, so you can map both numeric values to y.

reads_data_struc %>% 
    pivot_longer(cols = c(input, nonchimeric), 
                 names_to = "my_variable", 
                 values_to = "my_value") %>% 
    ggplot(aes(samplename, my_value, group = my_variable, fill = treatment)) +
    geom_bar(stat = "identity", position = "dodge") +
    theme(axis.text.x = element_text(angle = 30, vjust = .7)) 
geom_na
  • 258
  • 2
  • 10
  • Thank you very much @Nomad420! Your solution worked quite well. The only change I made is that I changed `fill = treatment` to `fill = my_variable` and added `facet_grid( ~ treatment, scales = "free")`. I have exactly what I need. Thank you! – tslb14 Jul 22 '21 at 18:58