0

I used the search function and surprisingly couldn't find a solution that worked here. I have the following dataset:

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

I then use this code to plot the data:

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

I have subsetted the data so that it only contains information from 2019-05-01 to 2019-09-06, yet the because of the 3 day brakes that I require, I cannot get the x axis to start on 2019-05-01. Is there a way to force this start date?

Thanks for any help you can provide and please let me know if there's any other information I should add or reformatting that I should do to my question.

Tom
  • 377
  • 3
  • 13
  • 3
    There are some issues with your code. First, `dateNew` should be added as a column to `data`, not stored as a vector. Second, a mismatched parenthesis in `scale_x_date`. Third, your date format is "%Y-%m-%d" not "%m/%d/%Y". – neilfws Dec 02 '20 at 23:16
  • 4
    You could try setting the limits argument inside `scale_x_date`. – Jonathan V. Solórzano Dec 02 '20 at 23:20
  • This might also be helpful: https://stackoverflow.com/questions/13701347/force-the-origin-to-start-at-0?rq=1 – Dylan_Gomes Dec 03 '20 at 00:02

1 Answers1

1

Your use of date_breaks is limiting you a little, but an alternative is to specify breaks= instead. For this, I'll use seq.Date (the Date S3 method of the seq function), and specify the start date and the "3 days" you need.

data$Date <- as.Date(data$Date)
ggplot(data, aes(Date, cm, fill=Type)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  xlab("Date") +
  ylab("Actual Water Received (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",
               breaks = seq(as.Date("2019-05-01"), as.Date("2019-09-11"), by = "3 days"))

ggplot2 with May 1 as the first date

r2evans
  • 141,215
  • 6
  • 77
  • 149