How to label months on julian day x axis in R

Viewed 1291

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!

1 Answers

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")

jday_axisplot

Related