R ggplot 限制绘图 x 轴上的日期

R ggplot limiting dates on plot x-axis

我正在尝试创建一个图表,其中 y 轴为体积,x 轴为应用周。但是,使用 ggplot 绘图会导致我的图表错位。我已经通过调整限制和扩展功能研究了提议的解决方案,但似乎仍然无法获得预期的结果。

下面是我生成图表的代码:

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))}

下面是一些示例数据:

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))

下面是生成的图表。最小日期为 12-28-2017,最大日期为 06-14-2018。但是,轴标签不会在那些日子开始或结束。

R 图表的结果图像:

此处有两个主要修复。首先是您的列 Week 未被识别为日期,因此请先解决该问题:

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

其次,scale_x_date() 中的论点不太正确。下面修改后的绘图代码应该可以满足您的需求。

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")