Home > Blockchain >  What changes can be made to code to get the graph that I wanted using ggplot2?
What changes can be made to code to get the graph that I wanted using ggplot2?

Time:12-30

I have a data frame "India_variant_df" and I'm trying to plot a multiline graph using the ggplot2 package. The graph that comes as output is not something that I imagined. I need help to figure out, where am I going wrong.

This is my df.

dput(India_variant_df)
    structure(list(month_year = c("Apr-2021", "Apr-2021", "Apr-2021", 
    "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", 
    "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", 
    "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", "Apr-2021", 
    "Apr-2021", "Apr-2021", "Apr-2021", "Aug-2020", "Aug-2020", "Aug-2020", 
    "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", 
    "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", 
    "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2020", 
    "Aug-2020", "Aug-2020", "Aug-2020", "Aug-2021", "Aug-2021", "Aug-2021", 
    "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", 
    "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", 
    "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", "Aug-2021", 
    "Aug-2021", "Aug-2021", "Aug-2021", "Dec-2020", "Dec-2020", "Dec-2020", 
    "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", 
    "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", 
    "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2020", 
    "Dec-2020", "Dec-2020", "Dec-2020", "Dec-2021", "Dec-2021", "Dec-2021", 
    "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", 
    "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", 
    "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", "Dec-2021", 
    "Dec-2021", "Dec-2021", "Dec-2021", "Feb-2021", "Feb-2021", "Feb-2021", 
    "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", 
    "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", 
    "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", "Feb-2021", 
    "Feb-2021", "Feb-2021", "Feb-2021", "Jan-2021", "Jan-2021", "Jan-2021", 
    "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", 
    "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", 
    "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", "Jan-2021", 
    "Jan-2021", "Jan-2021", "Jan-2021", "Jul-2020", "Jul-2020", "Jul-2020", 
    "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", 
    "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", 
    "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2020", 
    "Jul-2020", "Jul-2020", "Jul-2020", "Jul-2021", "Jul-2021", "Jul-2021", 
    "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", 
    "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", 
    "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", "Jul-2021", 
    "Jul-2021", "Jul-2021", "Jul-2021", "Jun-2020", "Jun-2020", "Jun-2020", 
    "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", 
    "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", 
    "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2020", 
    "Jun-2020", "Jun-2020", "Jun-2020", "Jun-2021", "Jun-2021", "Jun-2021", 
    "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", 
    "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", 
    "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", "Jun-2021", 
    "Jun-2021", "Jun-2021", "Jun-2021", "Mar-2021", "Mar-2021", "Mar-2021", 
    "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", 
    "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", 
    "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", "Mar-2021", 
    "Mar-2021", "Mar-2021", "Mar-2021", "May-2020", "May-2020", "May-2020", 
    "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", 
    "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", 
    "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", "May-2020", 
    "May-2020", "May-2020", "May-2020", "May-2021", "May-2021", "May-2021", 
    "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", 
    "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", 
    "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", "May-2021", 
    "May-2021", "May-2021", "May-2021", "Nov-2020", "Nov-2020", "Nov-2020", 
    "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", 
    "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", 
    "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2020", 
    "Nov-2020", "Nov-2020", "Nov-2020", "Nov-2021", "Nov-2021", "Nov-2021", 
    "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", 
    "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", 
    "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", "Nov-2021", 
    "Nov-2021", "Nov-2021", "Nov-2021", "Oct-2020", "Oct-2020", "Oct-2020", 
    "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", 
    "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", 
    "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2020", 
    "Oct-2020", "Oct-2020", "Oct-2020", "Oct-2021", "Oct-2021", "Oct-2021", 
    "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", 
    "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", 
    "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", "Oct-2021", 
    "Oct-2021", "Oct-2021", "Oct-2021", "Sep-2020", "Sep-2020", "Sep-2020", 
    "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", 
    "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", 
    "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2020", 
    "Sep-2020", "Sep-2020", "Sep-2020", "Sep-2021", "Sep-2021", "Sep-2021", 
    "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", 
    "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", 
    "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", "Sep-2021", 
    "Sep-2021", "Sep-2021", "Sep-2021"), variant = c("Alpha", "B.1.1.277", 
    "B.1.1.302", "B.1.1.519", "B.1.160", "B.1.177", "B.1.221", "B.1.258", 
    "B.1.367", "B.1.620", "Beta", "Delta", "Epsilon", "Eta", "Gamma", 
    "Iota", "Kappa", "Lambda", "Mu", "non_who", "Omicron", "others", 
    "S:677H.Robin1", "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", 
    "B.1.1.519", "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", 
    "B.1.620", "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", 
    "Kappa", "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican", "Alpha", "B.1.1.277", "B.1.1.302", "B.1.1.519", 
    "B.1.160", "B.1.177", "B.1.221", "B.1.258", "B.1.367", "B.1.620", 
    "Beta", "Delta", "Epsilon", "Eta", "Gamma", "Iota", "Kappa", 
    "Lambda", "Mu", "non_who", "Omicron", "others", "S:677H.Robin1", 
    "S:677P.Pelican"), num_seqs_of_variant = c(2035L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 121L, 5248L, 0L, 42L, 3L, 1L, 2741L, 
    0L, 0L, 2003L, 0L, 2003L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1046L, 0L, 1046L, 
    0L, 0L, 7L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 5355L, 0L, 
    0L, 1L, 0L, 5L, 0L, 0L, 107L, 0L, 107L, 0L, 0L, 18L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 31L, 0L, 0L, 0L, 0L, 8L, 0L, 
    0L, 2585L, 0L, 2585L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 617L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 18L, 64L, 18L, 
    0L, 0L, 262L, 0L, 0L, 0L, 0L, 6L, 0L, 1L, 0L, 0L, 11L, 156L, 
    0L, 7L, 0L, 0L, 237L, 0L, 0L, 2356L, 0L, 2349L, 0L, 0L, 134L, 
    0L, 0L, 0L, 1L, 10L, 0L, 5L, 0L, 0L, 3L, 84L, 0L, 0L, 0L, 0L, 
    28L, 0L, 0L, 2762L, 0L, 2745L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 593L, 0L, 
    593L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 6409L, 
    0L, 0L, 0L, 0L, 19L, 0L, 0L, 219L, 0L, 219L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1383L, 0L, 1383L, 0L, 0L, 28L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L, 9L, 7684L, 0L, 0L, 0L, 0L, 37L, 0L, 0L, 258L, 0L, 257L, 
    0L, 0L, 1672L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 87L, 380L, 
    1L, 50L, 0L, 0L, 1577L, 0L, 0L, 2461L, 0L, 2461L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1120L, 0L, 1120L, 0L, 0L, 639L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 53L, 16940L, 0L, 14L, 1L, 0L, 765L, 0L, 0L, 1387L, 
    0L, 1387L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    28L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 611L, 0L, 611L, 0L, 0L, 2L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4007L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 210L, 5L, 210L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 736L, 0L, 736L, 
    0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 4123L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 62L, 0L, 61L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 820L, 
    0L, 820L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    6738L, 0L, 0L, 0L, 0L, 15L, 0L, 0L, 73L, 0L, 72L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
    -480L))

This is my code.

ggplot(data = India_variant_df, aes(x= month_year, y = num_seqs_of_variant, 
                                    group = "variant", colour = variant))  
      geom_point(stat = "identity") 
      geom_line()

This is the graph I got from the code. enter image description here

This is the graph I kinda wanted.(I created using Excel pivot chart tool) enter image description here

CodePudding user response:

  1. As mentioned, use group = variant, not "variant".

  2. Your x axis is currently categorical (strings), not something number-like. Worse is that they are sorting lexicographically, so they are completely out of (intuitive) order:

    sort(unique(India_variant_df$month_year))
    #  [1] "Apr-2021" "Aug-2020" "Aug-2021" "Dec-2020" "Dec-2021" "Feb-2021"
    #  [7] "Jan-2021" "Jul-2020" "Jul-2021" "Jun-2020" "Jun-2021" "Mar-2021"
    # [13] "May-2020" "May-2021" "Nov-2020" "Nov-2021" "Oct-2020" "Oct-2021"
    # [19] "Sep-2020" "Sep-2021"
    

    (And ggplot is going to sort them for you.)

    Change this to a proper Date-class object with:

    India_variant_df$date <- as.Date(paste0("01-", India_variant_df$month_year), format = "%d-%b-%Y")
    head(India_variant_df)
    #   month_year   variant num_seqs_of_variant       date
    # 1   Apr-2021     Alpha                2035 2021-04-01
    # 2   Apr-2021 B.1.1.277                   0 2021-04-01
    # 3   Apr-2021 B.1.1.302                   0 2021-04-01
    # 4   Apr-2021 B.1.1.519                   0 2021-04-01
    # 5   Apr-2021   B.1.160                   0 2021-04-01
    # 6   Apr-2021   B.1.177                   0 2021-04-01
    
  3. In order to get the date labels you want, we need to use scale_x_date. The two arguments I'll use are date_breaks to set them at monthly intervals, and labels; the latter will take a little elbow-grease to get the depressed year, but I think it works. (I don't know of a way to get it to look exactly as your demo pic.)

    ggplot(data = India_variant_df, aes(x= date, y = num_seqs_of_variant, 
                                        group = variant, colour = variant))  
      geom_point(stat = "identity")  
      geom_line()  
      scale_x_date(
        date_breaks = "1 month",
        labels = function(z) ifelse(seq_along(z) == 2L | format(z, format="%m") == "01",
                                    format(z, format = "%b\n%Y"),
                                    format(z, "%b"))
      )
    

    The ifelse has two components worth mentioning in its attempt to add the year below certain months.

    • Below every January; for this, format(z, format="%m"), which is locale-safe (as far as spelling of January);
    • Since we don't know for certain that the first month shown is or is not January, I show the year under the first month displayed. This gives us a little context, and protects us in the unlikely event that the data only includes Feb-Dec in one year (for which the year would otherwise never be shown). For this, we use seq_along(z) == 2L. It's 2L because the first z passed to this anon-func is NA, so we go with the second. There could be other trickery to get this perfect (I don't recall offhand if the first element is always NA).

ggplot with properly ordered and formatted x-axis

(The rest of the theming is up to you :-)

CodePudding user response:

I removed the quotes and converted the %b-%Ycol into a Date col.

India_variant_df["month_year"] = as.Date(paste0("01-", India_variant_df[["month_year"]]), format = "%d-%b-%Y")
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            -480L))


ggplot(data = India_variant_df, aes(x= month_year, y = num_seqs_of_variant, 
                                    group = variant, colour = variant))  
  geom_point(stat = "identity") 
  geom_line()

Output:

enter image description here

  • Related