Search code examples
rdateggplot2limitslinegraph

R ggplot limiting dates on plot x-axis


I'm trying to create a chart with volume on y-axis and app week on x-axis. However, plotting using ggplot results in a misalignment on my chart. I've researched proposed solutions by tweaking limits and the expand functions but still can't seem to get the desired outcome.

Below is my code to generate the chart:

min_date = min(Data2$Week)
max_date = max(Data2$Week)

lims = c(min_date,max_date)

a = {Data2 %>%
    mutate(facet=ifelse(ChartGroup=='Test1','Test','Control')) %>%
    ggplot(aes(x=Week, y=Volume, group=ChartGroup, color=ChartGroup)) + 
    geom_line() + geom_point() + facet_grid(facet~.,scales='free_y') +
    theme( axis.text.x = element_text(angle = 90)) +
    labs(title="Volume") + 
    labs(x= "Week") + 
    labs(y ="Volume") +
    scale_x_date(breaks=date_breaks("week"),
                 labels = date_format("%Y-%m-%d"),
                 limits=lims,
                 expand = c(0,0))}

Here's some sample data:

structure(list(ChartGroup = c("Test1", "Test1", "Test1", "Test1", 
"Test1", "Test1", "Test1", "Test1", "Test1", "Test1", "Test1", 
"Test1", "Test1", "Test1", "Test1", "Test1", "Test1", "Test1", 
"Test1", "Test1", "Test1", "Test1", "Test1", "Test1", "Test1", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", 
"Test", "Test"), Week = c("12/28/2017", "1/4/2018", "1/11/2018", 
"1/18/2018", "1/25/2018", "2/1/2018", "2/8/2018", "2/15/2018", 
"2/22/2018", "3/1/2018", "3/8/2018", "3/15/2018", "3/22/2018", 
"3/29/2018", "4/5/2018", "4/12/2018", "4/19/2018", "4/26/2018", 
"5/3/2018", "5/10/2018", "5/17/2018", "5/24/2018", "5/31/2018", 
"6/7/2018", "6/14/2018", "12/28/2017", "12/28/2017", "1/4/2018", 
"1/4/2018", "1/11/2018", "1/11/2018", "1/18/2018", "1/18/2018", 
"1/25/2018", "1/25/2018", "2/1/2018", "2/1/2018", "2/8/2018", 
"2/8/2018", "2/15/2018", "2/15/2018", "2/22/2018", "2/22/2018", 
"3/1/2018", "3/1/2018", "3/8/2018", "3/8/2018", "3/15/2018", 
"3/15/2018", "3/22/2018", "3/22/2018", "3/29/2018", "3/29/2018", 
"4/5/2018", "4/5/2018", "4/12/2018", "4/12/2018", "4/19/2018", 
"4/19/2018", "4/26/2018", "4/26/2018", "5/3/2018", "5/3/2018", 
"5/10/2018", "5/10/2018", "5/17/2018", "5/17/2018", "5/24/2018", 
"5/24/2018", "5/31/2018", "5/31/2018", "6/7/2018", "6/7/2018", 
"6/14/2018", "6/14/2018"), Volume = c(273.0894758, 885.410888, 
1123.424761, 940.204851, 1015.8218, 1595.801905, 167.8867937, 
792.0141589, 915.0446468, 2307.141323, 2277.582562, 1303.148027, 
1987.675449, 189.8547385, 102.8398224, 1122.591571, 615.5550876, 
1123.017699, 1680.158398, 2023.198433, 1206.52614, 1935.904725, 
1028.85108, 2259.716499, 1820.035456, 6748.379814, 19263.34329, 
20263.55385, 8616.685352, 36268.79244, 8369.983757, 46668.11793, 
15634.93125, 25826.72827, 15203.71984, 11781.08351, 14527.59158, 
45597.59167, 7459.457733, 46060.49379, 19793.76319, 58165.58381, 
18497.82931, 48976.75836, 35195.1871, 32711.4056, 51272.6735, 
17267.50781, 38880.17827, 31562.78799, 15184.4221, 33993.32688, 
27081.21869, 53983.76112, 38520.25106, 49381.99962, 4233.203344, 
2556.980954, 8367.881436, 15473.37447, 21542.29983, 35065.2121, 
26370.52407, 7564.324054, 9969.176408, 50594.20126, 14232.09825, 
38125.67594, 36392.11016, 15048.03106, 25569.13761, 1464.704752, 
25366.02593, 25365.96216, 236.938689)), class = "data.frame", row.names = c(NA, 
-75L))

Below is the resulting chart. Min date is 12-28-2017 and max date is 06-14-2018. However, the axis labels do not start or end on those days.

Resulting image of chart on R:


Solution

  • Two main fixes here. First is that your column Week is not recognized as a date, so fix that first:

    Data2$Week <- as.Date(Data2$Week, format="%m/%d/%Y")
    

    Second, arguments in scale_x_date() weren't quite right. Revised plotting code below should get you what you're looking for.

    ggplot(a, aes(x=Week, y=Volume, group=ChartGroup, color=ChartGroup)) + 
      geom_line() + 
      geom_point() + 
      facet_grid(facet~.,scales='free_y') +
      theme(axis.text.x = element_text(angle = 90)) +
      labs(title="Volume") + 
      labs(x= "Week") + 
      labs(y ="Volume") +
      scale_x_date(breaks = seq(as.Date(lims[1]), as.Date(lims[2]), by="1 week"), date_labels = "%Y-%m-%d")
    

    enter image description here