Search code examples
rggplot2axis-labelsposixct

Changing POSICXT x-axis to selected labels


I have temperature data taken every 30 minutes for 19 days (September 11th to 30th) and I'd like to change my x-axis labels to show each day or every other day and label it "Day 1", "Day 3", (...), instead of only three days labeled by date ("Sep 14") as shown in the next link: temperature plot.

enter image description here

Time data is in POSIXCT class. I'm attaching the ggplot coding I used. I'm leaving #scale_x_datetime to show one of my failed solutions.

hoboplot <- ggplot(hobo, aes(x=time, y=Temperatura, color=Tratamiento))+
  geom_path(size=0.86)+
  scale_color_manual(values=c("#2166AC", "#92C5DE", "#FDDBC7", "#D6604D", "#B2182B"))+
#  scale_x_datetime(labels=c("1", "3", "5", "7", "9", "11", "13", "15", "17", "19"),
#                 breaks = c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19))+
  theme_classic(base_size=18)+
  labs(x="Días", y="Temperatura (°C)")+
  geom_hline(yintercept=10, linetype="dashed", color = "#2166AC")+
  geom_hline(yintercept=14, linetype="dashed", color = "#92C5DE")+
  geom_hline(yintercept=18, linetype="dashed", color = "#FDDBC7")+
  geom_hline(yintercept=22, linetype="dashed", color = "#D6604D")+
  geom_hline(yintercept=26, linetype="dashed", color = "#B2182B")+
  theme(legend.position = "right")+
  guides(color=guide_legend(reverse=TRUE))  

I'd appreciate any help!


Solution

  • Try using -

    + scale_x_datetime(date_breaks = '2 days', 
                       labels  = function(x) paste('Day', as.Date(x) - min(as.Date(x), na.rm = TRUE) + 1))