Displaying Both Dates and Numeric Units on the X-Axis in R (feat. patchwork package)
When we create time series graphs in R, it is sometimes necessary to display both dates and numbers on the x-axis. This is because when the x-axis is set to show dates only, it can be challenging to add text or other elements directly onto the graph. However, by using both dates and numbers on the x-axis, we can easily insert texts, lines, and other annotations.
Let’s talk with data.
dataA=data.frame(
structure(list(Sampling_date=
structure(c(1684368000, 1685318400,
1685923200, 1686528000, 1687737600,
1688256000, 1688774400, 1689552000,
1690156800, 1691366400, 1691971200,
1693094400, 1694131200, 1694908800,
1695340800),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Day = c(0, 11, 18, 25, 39, 45, 51, 60, 67, 81,
88, 101, 113, 122, 127),
Gas_Flux = c(2, 2.54, 1.96, 0.65, 0.88, 0.93,
0, 0.75, 0.68, 0.84, 0.78, 0.91, 3.42, 2.3, 0.94)),
class = c("tbl_df","tbl", "data.frame"),
row.names = c(NA, -15L)))
Sampling_date Day Gas_Flux
1 2023-05-18 0 2.00
2 2023-05-29 11 2.54
3 2023-06-05 18 1.96
4 2023-06-12 25 0.65
5 2023-06-26 39 0.88
6 2023-07-02 45 0.93
7 2023-07-08 51 0.00
8 2023-07-17 60 0.75
9 2023-07-24 67 0.68
10 2023-08-07 81 0.84
11 2023-08-14 88 0.78
12 2023-08-27 101 0.91
13 2023-09-08 113 3.42
14 2023-09-17 122 2.30
15 2023-09-22 127 0.94
and I made a line graph over date.
library(ggplot2)
ggplot(data=dataA, aes(x=Sampling_date, y=Gas_Flux))+
geom_line (size=0.5)+
geom_point(size=5, alpha=0.5) +
scale_x_datetime(date_breaks = "14 day", date_labels = "%b/%d") +
scale_y_continuous(breaks = seq(0,5,1), limits = c(0,5)) +
theme_classic (base_size=20, base_family="serif")+
theme(legend.position=c(0.93,0.10),
legend.title=element_blank(),
legend.key=element_rect(color=alpha("grey",.05),
fill=alpha("grey",.05)),
legend.background= element_rect(fill=alpha("grey",.05)),
axis.line=element_line(linewidth=0.5, colour="black"))+
windows(width=12, height=8)
But I also want to display the corresponding experiment day for each date. Therefore, I would like to set up the day data (in numeric format) as the primary x-axis, and the date data (in date format) as the secondary x-axis.
library(patchwork)
day= ggplot(data=dataA, aes(x=Day, y=Gas_Flux))+
geom_line (size=0.5)+
geom_point(size=5, alpha=0.5) +
scale_x_continuous(breaks = scales::breaks_width(14)) +
scale_y_continuous(breaks = seq(0,5,1), limits = c(0,5)) +
theme_classic (base_size=20, base_family="serif")+
theme(legend.position=c(0.93,0.10),
legend.title=element_blank(),
legend.key=element_rect(color=alpha("grey",.05),
fill=alpha("grey",.05)),
legend.background= element_rect(fill=alpha("grey",.05)),
panel.grid.major=element_line(colour="grey90", linewidth=0.5),
axis.line=element_line(linewidth=0.5, colour="black"))
date= ggplot(data=dataA, aes(x=Sampling_date, y=Gas_Flux))+
scale_x_datetime(date_breaks = "14 day", date_labels = "%b/%d") +
theme_classic (base_size=20, base_family="serif")+
theme(axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y =element_blank(),
axis.line=element_line(linewidth=0.5, colour="black"))
Fig= day/date + plot_layout(heights = c(30,1))
Fig + windows(width=12, height=7)
Now, I have set up the numeric values as the primary x-axis and dates as the secondary x-axis. With the x-axis in numeric format, we can easily add annotations to the graphs.
library(patchwork)
day= ggplot(data=dataA, aes(x=Day, y=Gas_Flux))+
geom_line (size=0.5)+
geom_point(size=5, alpha=0.5) +
geom_vline(xintercept=70, linetype="dashed", color="black") +
geom_label(label="text added", family="serif", fontface=6, color="black",
x=70, y=5, size=4.5, fill="white") +
scale_x_continuous(breaks = scales::breaks_width(14)) +
scale_y_continuous(breaks = seq(0,5,1), limits = c(0,5)) +
theme_classic (base_size=20, base_family="serif")+
theme(legend.position=c(0.93,0.10),
legend.title=element_blank(),
legend.key=element_rect(color=alpha("grey",.05), fill=alpha("grey",.05)),
legend.background= element_rect(fill=alpha("grey",.05)),
panel.grid.major=element_line(colour="grey90", linewidth=0.5),
axis.line=element_line(linewidth=0.5, colour="black"))
date= ggplot(data=dataA, aes(x=Sampling_date, y=Gas_Flux))+
scale_x_datetime(date_breaks = "14 day", date_labels = "%b/%d") +
theme_classic (base_size=20, base_family="serif")+
theme(axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y =element_blank(),
axis.line=element_line(linewidth=0.5, colour="black"))
Fig= day/date + plot_layout(heights = c(30,1))
Fig + windows(width=12, height=7)
If the x-axis is set to show dates only, it would be much more difficult to add texts, lines, and other annotations. Therefore, using this method to display both numeric values and dates on the x-axis allows for easier addition of texts, lines, and other annotations.