R 如何强制ggplot在特定日期开始?

R 如何强制ggplot在特定日期开始?,r,ggplot2,R,Ggplot2,我使用了搜索功能,但令人惊讶的是,在这里找不到有效的解决方案。我有以下数据集: data Date Type Treatment Inches cm 2 2019-05-06 Precipitation 100% 0.06 0.1524 3 2019-05-09 Precipitation 100% 0.65 1.6510 4 2019-05-10 Precipitation 100% 0.20 0.50

我使用了搜索功能,但令人惊讶的是,在这里找不到有效的解决方案。我有以下数据集:

data
         Date          Type Treatment Inches     cm
2  2019-05-06 Precipitation      100%   0.06 0.1524
3  2019-05-09 Precipitation      100%   0.65 1.6510
4  2019-05-10 Precipitation      100%   0.20 0.5080
5  2019-05-14    Irrigation      100%   0.25 0.6350
6  2019-05-18 Precipitation      100%   0.41 1.0414
7  2019-05-19 Precipitation      100%   0.59 1.4986
8  2019-05-22 Precipitation      100%   0.30 0.7620
9  2019-05-23 Precipitation      100%   0.03 0.0762
10 2019-05-23    Irrigation      100%   0.30 0.7620
11 2019-05-24 Precipitation      100%   0.11 0.2794
12 2019-05-26    Irrigation      100%   0.30 0.7620
13 2019-05-28 Precipitation      100%   1.81 4.5974
14 2019-05-30    Irrigation      100%   0.40 1.0160
15 2019-06-02    Irrigation      100%   0.35 0.8890
16 2019-06-02 Precipitation      100%   0.15 0.3810
17 2019-06-04    Irrigation      100%   0.35 0.8890
18 2019-06-05 Precipitation      100%   0.22 0.5588
19 2019-06-07    Irrigation      100%   0.40 1.0160
20 2019-06-09    Irrigation      100%   0.40 1.0160
21 2019-06-10 Precipitation      100%   0.24 0.6096
22 2019-06-11    Irrigation      100%   0.25 0.6350
23 2019-06-12 Precipitation      100%   0.40 1.0160
24 2019-06-13 Precipitation      100%   0.32 0.8128
25 2019-06-14    Irrigation      100%   0.30 0.7620
26 2019-06-14 Precipitation      100%   0.03 0.0762
27 2019-06-15 Precipitation      100%   1.04 2.6416
28 2019-06-16 Precipitation      100%   0.79 2.0066
29 2019-06-19    Irrigation      100%   0.35 0.8890
30 2019-06-19 Precipitation      100%   0.03 0.0762
31 2019-06-21    Irrigation      100%   0.35 0.8890
32 2019-06-23    Irrigation      100%   0.30 0.7620
33 2019-06-24 Precipitation      100%   0.12 0.3048
34 2019-06-25    Irrigation      100%   0.35 0.8890
35 2019-06-25 Precipitation      100%   0.26 0.6604
36 2019-06-27    Irrigation      100%   0.40 1.0160
37 2019-06-28 Precipitation      100%   1.39 3.5306
38 2019-06-30    Irrigation      100%   0.35 0.8890
39 2019-07-01 Precipitation      100%   0.78 1.9812
40 2019-07-02 Precipitation      100%   1.60 4.0640
41 2019-07-03 Precipitation      100%   0.02 0.0508
42 2019-07-04    Irrigation      100%   0.35 0.8890
43 2019-07-05 Precipitation      100%   0.64 1.6256
44 2019-07-06 Precipitation      100%   0.07 0.1778
45 2019-07-07    Irrigation      100%   0.35 0.8890
46 2019-07-09    Irrigation      100%   0.40 1.0160
47 2019-07-11    Irrigation      100%   0.50 1.2700
48 2019-07-13    Irrigation      100%   0.50 1.2700
49 2019-07-15    Irrigation      100%   0.50 1.2700
50 2019-07-16 Precipitation      100%   0.35 0.8890
51 2019-07-17    Irrigation      100%   0.60 1.5240
52 2019-07-18 Precipitation      100%   0.23 0.5842
53 2019-07-19    Irrigation      100%   0.50 1.2700
54 2019-07-19 Precipitation      100%   0.07 0.1778
55 2019-07-20 Precipitation      100%   1.60 4.0640
56 2019-07-21 Precipitation      100%   1.21 3.0734
57 2019-07-23    Irrigation      100%   0.40 1.0160
58 2019-07-25    Irrigation      100%   0.40 1.0160
59 2019-07-27    Irrigation      100%   0.50 1.2700
60 2019-07-29 Precipitation      100%   0.61 1.5494
61 2019-07-30    Irrigation      100%   0.40 1.0160
62 2019-08-01    Irrigation      100%   0.50 1.2700
63 2019-08-03    Irrigation      100%   0.50 1.2700
64 2019-08-05    Irrigation      100%   0.40 1.0160
65 2019-08-05 Precipitation      100%   0.12 0.3048
66 2019-08-06 Precipitation      100%   0.98 2.4892
67 2019-08-08 Precipitation      100%   0.37 0.9398
68 2019-08-10    Irrigation      100%   0.40 1.0160
69 2019-08-11 Precipitation      100%   0.29 0.7366
70 2019-08-12 Precipitation      100%   0.13 0.3302
71 2019-08-13 Precipitation      100%   0.01 0.0254
72 2019-08-14 Precipitation      100%   0.41 1.0414
73 2019-08-16 Precipitation      100%   0.12 0.3048
74 2019-08-18 Precipitation      100%   0.22 0.5588
75 2019-08-19    Irrigation      100%   0.40 1.0160
76 2019-08-22    Irrigation      100%   0.40 1.0160
77 2019-08-26 Precipitation      100%   0.02 0.0508
78 2019-08-27    Irrigation      100%   0.35 0.8890
79 2019-08-27 Precipitation      100%   0.10 0.2540
80 2019-08-28 Precipitation      100%   0.11 0.2794
81 2019-08-29    Irrigation      100%   0.40 1.0160
82 2019-09-02    Irrigation      100%   0.50 1.2700
83 2019-09-03 Precipitation      100%   0.41 1.0414
84 2019-09-04 Precipitation      100%   0.02 0.0508
85 2019-09-06    Irrigation      100%   0.30 0.7620
然后,我使用此代码绘制数据:

dateNew <- as.Date(data$Date, format='%m/%d/%Y')



data %>% ggplot(aes(dateNew,cm,fill=Type)) + geom_bar(stat='identity',position = 'dodge') +
  theme(axis.text.x = element_text(angle = 45,hjust=1)) + 
  xlab("Date") +
  ylab("Actual Water Recieved (Cm)") +
  ggtitle("2019") +
  scale_fill_grey()+
  theme_bw(base_size = 18) +
  theme(axis.text.x = element_text(angle = 80, hjust = 1))+
  scale_x_date(date_labels="%b %d",date_breaks="3 days"))
dateNew%ggplot(aes(dateNew,cm,fill=Type))+geom_条(stat='identity',position='dodge')+
主题(axis.text.x=元素\文本(角度=45,hjust=1))+
xlab(“日期”)+
ylab(“实际接收水量(Cm)”)+
标题(“2019年”)+
比例\填充\灰色()+
主题尺寸(基本尺寸=18)+
主题(axis.text.x=元素\文本(角度=80,hjust=1))+
缩放日期(日期标签=“%b%d”,日期间隔=“3天”))
我已对数据进行了子集设置,以便其仅包含2019-05-01至2019-09-06的信息,但由于我需要3天的制动,我无法使x轴在2019-05-01开始。有没有办法强制这个开始日期


感谢您提供的任何帮助,如果有任何其他信息需要添加或重新格式化,请告诉我。

您使用的
日期中断
会对您造成一些限制,但另一种方法是指定
中断=
。为此,我将使用
seq.Date
seq
函数的
Date
S3方法),并指定所需的开始日期和
“3天”


data$Date您的代码存在一些问题。首先,
dateNew
应作为列添加到
数据中,而不是作为向量存储。其次,
scale\u x\u date
中的括号不匹配。第三,您的日期格式是“%Y-%m-%d”而不是“%m/%d/%Y”。您可以尝试在
scale\u x\u date
中设置限制参数。这也可能有帮助: