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

时间:2018-06-22 01:36:14

标签: r date ggplot2 limits linegraph

我正在尝试创建一个图表,其中y轴为成交量,x轴为app周。但是,使用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))

以下是结果图表。最小日期是2017年12月28日,最大日期是2018年6月14日。但是,轴标签在那几天不会开始或结束。

R上图表的结果图像:

1 个答案:

答案 0 :(得分:0)

此处有两个主要修复程序。首先是您的列[√] Flutter (Channel dev, v0.5.4, on Microsoft Windows [Version 10.0.17134.112], locale en-GB) • Flutter version 0.5.4 at C:\VirtualDrives\Programs\flutter • Framework revision 3019ad976d (11 days ago), 2018-06-11 11:31:25 -0700 • Engine revision d33bbff470 • Dart version 2.0.0-dev.60.0.flutter-a5e41681e5 [√] Android toolchain - develop for Android devices (Android SDK 27.0.3) • Android SDK at C:\VirtualDrives\Programs\Android\sdk • Android NDK location not configured (optional; useful for native profiling support) • Platform android-27, build-tools 27.0.3 • ANDROID_HOME = C:\VirtualDrives\Programs\Android\sdk • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02) • All Android licenses accepted. [√] Android Studio (version 3.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin version 25.0.1 • Dart plugin version 173.4700 • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02) [√] IntelliJ IDEA Community Edition (version 2017.2) • IntelliJ at C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2017.1 • Flutter plugin version 19.1 • Dart plugin version 172.4343.25 [√] IntelliJ IDEA Community Edition (version 2018.1) • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1 • Flutter plugin version 25.0.2 • Dart plugin version 181.4892.1 [!] VS Code, 32-bit edition • VS Code at C:\Program Files (x86)\Microsoft VS Code • Flutter extension not installed; install from https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [!] VS Code, 64-bit edition (version 1.22.2) • VS Code at C:\Program Files\Microsoft VS Code • Flutter extension not installed; install from https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [!] Connected devices ! No devices available ! Doctor found issues in 3 categories. 未被识别为日期,因此请首先解决该问题:

Week

第二,Data2$Week <- as.Date(Data2$Week, format="%m/%d/%Y") 中的参数不太正确。下面修改的绘图代码应能为您提供所需的东西。

scale_x_date()

enter image description here