I am making a plot (in ggplot2) where the x axis ranges from julian day 1-365. Is there any way to add month labels and tick marks to a julian day axis?
(i.e.,scale_x_date)
Thanks!
I am making a plot (in ggplot2) where the x axis ranges from julian day 1-365. Is there any way to add month labels and tick marks to a julian day axis?
(i.e.,scale_x_date)
Thanks!
You were on the right track with scale_x_date(), but your Julian days do need to be formatted as Dates first. The trick is selecting an origin that makes sense for your data, and then using scale_x_date() to format the labels:
library(ggplot2)
# make data
value <- rnorm(365, mean = 0, sd = 5)
jday <- 1:365 # represents your Julian days 1-365; assumes no leap years
# make data frame and add julian day
d <- data.frame(jday = jday, value = value, stringsAsFactors = FALSE)
head(d)
# plot with date labels on x axis
ggplot(d, aes(x = as.Date(jday, origin = as.Date("2018-01-01")), y = value)) +
geom_line() +
scale_x_date(date_labels = "%b")