how to make a two lines x-axis (year and date) using ggh4x? #133
Answered
by
teunbrand
yippiekahee
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
teunbrand
Nov 3, 2023
Replies: 1 comment 1 reply
-
You can use the library(dplyr)
library(ggplot2)
library(ggh4x)
start_date <- as.Date("2022-01-01")
end_date <- as.Date("2023-12-31")
random_dates <- sample(seq(start_date, end_date, by="days"), 10, replace=TRUE)
random_values <- runif(10, min = 0, max = 20)
df <- data.frame(date = random_dates, value = random_values)
df %<>%
mutate(date = as.Date(date)) %>%
mutate(year = format(date, "%Y"))
ggplot(df) +
geom_point(aes(x = date, y = value, group = 1)) +
scale_x_date(
date_labels = "%d-%m+%Y",
guide = guide_axis_nested(delim = "+")
) Created on 2023-11-03 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
yippiekahee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
date_breaks
argument inscale_x_date()
to separate year from day/month with a special delimiter, and then letguide_axis_nested()
know about this delimiter.