My dataset is a dataframe with max daily temp of water over the past 4 summers.
X site DateTime value Month Year Day
<int> <fct> <dttm> <dbl> <int> <fct> <chr>
1 6775 RAYNER_UP 2018-07-09 19:00:00 19.8 7 2018 07/10
2 6776 RAYNER_UP 2018-07-10 19:00:00 21.2 7 2018 07/11
3 6777 RAYNER_UP 2018-07-11 19:00:00 20.4 7 2018 07/12
4 6778 RAYNER_UP 2018-07-12 19:00:00 20.1 7 2018 07/13
5 6779 RAYNER_UP 2018-07-13 19:00:00 17.3 7 2018 07/14
6 6780 RAYNER_UP 2018-07-14 19:00:00 19.5 7 2018 07/15
7 6781 RAYNER_UP 2018-07-15 19:00:00 21.2 7 2018 07/16
8 6782 RAYNER_UP 2018-07-16 19:00:00 21.0 7 2018 07/17
9 6783 RAYNER_UP 2018-07-17 19:00:00 19.8 7 2018 07/18
10 6784 RAYNER_UP 2018-07-18 19:00:00 16.8 7 2018 07/19
My goal is to make a line graph with a different line for each year. So far, with a lot of help from the internet, I've made a line for each year BUT the scale is terribly packed together example.
I would like to rescale the x axis in my time series to show days of the month.
So far using scale_x_datetime, and scale_x_date in ggplot but have been unsuccessful. I keep getting the error: Invalid input: time_trans works with objects of class POSIXct only, even though the date column is in POSIXct.
Example of my code which is giving the probelm:
test1 = ggplot()
geom_line(data = Rayner_up_summer, aes(x=strftime(DateTime,format="%m/%d"),
y=value,
group = Year,
color=strftime(DateTime,format="%Y")))
# size=.1))
scale_color_discrete(name="Year")
labs(x="date")
Does anyone know why I am unable to use scale_x_datetime even though my x axis (DateTime) is in POSIXct Date Time format? I would appreciate any advice you might have
CodePudding user response:
As I said in my comment, the issue is that you convert your DateTime
column to a character via strftime(DateTime,format="%m/%d")
which is the reason why your scale get terribly packed together and also the reason why scale_x_date
or _datetime
will not work.
Instead, one option to achieve your desired result would be
- Add a helper date column to your data where you use the same year.
- Map this helper date column on
x
Note: I slightly altered your dataset to account for a second year.
Rayner_up_summer$DateTime <- as.Date(paste0("2018/", Rayner_up_summer$Day))
library(ggplot2)
ggplot()
geom_line(data = Rayner_up_summer, aes(
x = DateTime,
y = value,
group = Year,
color = factor(Year)
))
scale_x_date(date_labels = "%m/%d")
scale_color_discrete(name = "Year")
labs(x = "date")
DATA
Rayner_up_summer <- structure(list(X = c(
6775L, 6776L, 6777L, 6778L, 6779L, 6780L,
6781L, 6782L, 6783L, 6784L, 6775L, 6776L, 6777L, 6778L, 6779L,
6780L, 6781L, 6782L, 6783L, 6784L
), site = c(
"RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP", "RAYNER_UP",
"RAYNER_UP", "RAYNER_UP", "RAYNER_UP"
), DateTime = structure(c(
1531173600,
1531260000, 1531346400, 1531432800, 1531519200, 1531605600, 1531692000,
1531778400, 1531864800, 1531951200, 1531173600, 1531260000, 1531346400,
1531432800, 1531519200, 1531605600, 1531692000, 1531778400, 1531864800,
1531951200
), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(
19.8,
21.2, 20.4, 20.1, 17.3, 19.5, 21.2, 21, 19.8, 16.8, 21.5740302174818,
21.6853770664893, 18.4306976739317, 21.1522381303366, 20.208727594465,
19.5954797456507, 20.6829415732063, 17.6733329861891, 20.2849614520092,
20.5253239201847
), Month = c(
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), Year = c(
2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019
), Day = c(
"07/10",
"07/11", "07/12", "07/13", "07/14", "07/15", "07/16", "07/17",
"07/18", "07/19", "07/10", "07/11", "07/12", "07/13", "07/14",
"07/15", "07/16", "07/17", "07/18", "07/19"
)), row.names = c(
"1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "21", "31",
"41", "51", "61", "71", "81", "91", "101"
), class = "data.frame")