0

I have a large time series of temperature observations that I am attempting to visualize using ggplot in R. The temperature observations were collected at 20-minute intervals over a period of months resulting in 9048 data points. I can visualize the entire time series, but I would like to visualize the data by having a plot for each day in order to analyze any fine-scale features in my dataset.

I was able to plot the first day of data using the code below, but I do not know how to best go about setting up a loop or using another technique to split up the rest of the data easily so they can be plotted. Am I overthinking this?

p <- ggplot(Kappa_Temp,aes(x=TIME_UTC,y=TEMPERATURE))+
   geom_line(color="red")+
   geom_point(color="red")+
   xlab("")+
   theme_ipsum()+
   theme(axis.text.x=element_text(angle=60, hjust=1))+
   scale_x_datetime(limit=c(TIME_UTC[1],TIME_UTC[72]))
 p

1 Answers1

0

You can create a date variable to indicate the date/day, and use lapply() to loop over each of the unique date values, like this:

Kappa_Temp <- Kappa_Temp %>% mutate(date= as.Date(TIME_UTC))

plots <- lapply(unique(Kappa_Temp$date), function(d) {
  ggplot(Kappa_Temp %>% filter(date == d),aes(x=TIME_UTC,y=TEMPERATURE))+
    geom_line(color="red")+
    geom_point(color="red")+
    xlab("")+
    theme_ipsum()+
    theme(axis.text.x=element_text(angle=60, hjust=1))
})

plots is now a list of plots, with one plot for each day.

You could also use facet_wrap(~day, scales="free") to see multiple dates at one time; Note that this will create one facet for each day, so you may have to add some constraint to the data input to ggplot() to limit the number of facets (i.e. may be too many to look at one time).

ggplot(Kappa_Temp,aes(x=TIME_UTC,y=TEMPERATURE))+
  geom_line(color="red")+
  geom_point(color="red")+
  xlab("")+
  facet_wrap(~date) + 
  theme_ipsum()+
  theme(axis.text.x=element_text(angle=60, hjust=1))
langtang
  • 22,248
  • 1
  • 12
  • 27