I cannot get geom_line
to display a time series from oldest to newest when date is a factor.
(Similar SO Question here for geom_boxplot
.
Example Dataset
library(dplyr)
df1 <- data.frame(matrix(ncol = 3, nrow = 12))
colnames(df1)[1:3] <- c("Date", "Group", "Value")
df1$Date <- rep(seq.Date(as.Date("2020-03-14"),as.Date("2020-08-20"),"1 month"),2)
df1$Group <- sort(rep(c("A","B"),6))
df1$Value <- rnorm(12,50,20)
df1 <- df1 %>%
mutate(Month = month(Date),
Year = year(Date),
date = zoo::as.yearmon(paste(Year, Month), "%Y %m"))
df2 <- data.frame(matrix(ncol = 3, nrow = 12))
colnames(df2)[1:3] <- c("Date", "Group", "Value")
df2$Date <- rep(seq.Date(as.Date("2021-03-14"),as.Date("2021-08-20"),"1 month"),2)
df2$Group <- sort(rep(c("A","B"),6))
df2$Value <- rnorm(12,50,20)
df2 <- df2 %>%
mutate(Month = month(Date),
Year = year(Date),
date = zoo::as.yearmon(paste(Year, Month), "%Y %m"))
df3 <- rbind(df1,df2)
In this plot, the date is in alphabetical order. I need it to be in chronological order.
library(ggplot2)
cols <- c("A" = "#ca0020", "B" = "#0571b0")
ggplot(data = df1, aes(x = factor(date), y = Value, color = Group, group = Group)) +
geom_line() +
geom_point(size = 3, aes(fill = Group), color = "black", shape = 21) +
geom_line(data = df2, aes(x = factor(date), y = Value, color = Group, group = Group)) +
geom_point(data = df2, aes(x = factor(date), y = Value, fill = Group), color = "black", shape = 21, size = 3) +
scale_fill_manual(values = cols) +
scale_color_manual(values = cols) +
labs(x = "") +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
axis.text.y = element_text(size = 14, color = "black"),
legend.title = element_blank(),
legend.direction = "horizontal",
legend.margin = margin(),
legend.position = c(0.75,0.95))
This code works when I look at one year at a time. I know I could display each year with a facet_wrap()
but I need the data displayed on one figure.
ggplot(data = df1, aes(x = factor(date), y = Value, color = Group, group = Group)) +
geom_line() +
geom_point(size = 3, aes(fill = Group), color = "black", shape = 21) +
scale_fill_manual(values = cols) +
scale_color_manual(values = cols) +
labs(x = "") +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
axis.text.y = element_text(size = 14, color = "black"),
legend.title = element_blank(),
legend.direction = "horizontal",
legend.margin = margin(),
legend.position = c(0.75,0.95))
I have also tried this, but the problem with this one is that the line connecting year 2020
and 2021
should be broken.
ggplot(data = df3, aes(x = factor(date), y = Value, color = Group, group = Group)) +
geom_line() +
geom_point(size = 3, aes(fill = Group), color = "black", shape = 21) +
scale_fill_manual(values = cols) +
scale_color_manual(values = cols) +
labs(x = "") +
theme_bw() +
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
axis.text.y = element_text(size = 14, color = "black"),
legend.title = element_blank(),
legend.direction = "horizontal",
legend.margin = margin(),
legend.position = c(0.75,0.95))
The ideal output would look something like this; notice the line connecting data points breaks across years. I am using x = factor(date)
because I will eventually add an x-axis break which requires the axis to be a factor.