Get startedGet started for free

Plotting an aggregated time series with ggplot2

Aggregating data allows you to uncover general patterns and trends in your data, but can often lead to a loss of information and context. However, using methods from ggplot2 can give some context back to the aggregated data.

In this exercise, you'll practice plotting weekly aggregated temperature readings, weekly_avg, alongside the original, unaggregated time series, hourly_temperature, which represents temperature readings for an entire year, sampled every hour.

The time series hourly_temperature and weekly_avg, as well as the ggplot2 and zoo packages are available to you.

This exercise is part of the course

Manipulating Time Series Data in R

View Course

Exercise instructions

  • Using the ggplot() function, plot the hourly_temperature time series as a line plot.

  • Add the y-axis label "Degrees Celsius" and the title "Temperature Readings".

  • Complete the second call to geom_line() and aes() to overlay the weekly_avg time series on your plot.

  • Change the line color of the weekly aggregate to red and the size of the line to 2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a plot of the hourly_temperature time series
ggplot(___, aes(___)) + 
  ___ + 
  scale_y_continuous() + 
  
  # Add axis label and title
  labs(___) + 

  # Add a line plot for the weekly aggregated time series
  geom_line(data = ___, aes(___),
            
  # Color the aggregated line in red, with a size of 2
            ___) 
Edit and Run Code