Skip to Tutorial Content

Time series

Let's explore time series data by working with the Lincoln temperature data used in Fundamentals in Data Visualization. First, let's import these data and convert the dates to a date format.

lincoln_weather <- read_csv("lincoln_weather.csv") |> 
  mutate(date = as.Date(date))
head(lincoln_weather)

We have maximum, minimum, and mean temperature for every day in 2016. We'll start by plotting daily mean temperatures as points.

lincoln_weather |> 
  ggplot(aes(x = date, y = mean_temp)) +
  geom_point()

Using lines

Change the geometric object from points to a line.

lincoln_weather |> 
  ...
lincoln_weather |> 
  ggplot(aes(x = date, y = mean_temp)) +
  geom_line()

Multiple series

Let's say that we want to visualize the minimum and maximum rather than the mean. We can't do this with the current data frame because they are in different columns.

Reshape the data frame to put the minimum and maximum (only) temperatures into a single column called temp and the names in a column called measure. Call the new data frame lincoln_minmax_weather.

lincoln_minmax_weather <- 
  ...
lincoln_minmax_weather <- lincoln_weather |> 
  pivot_longer(c("min_temp", "max_temp"), names_to = "measure", values_to = "temp")

Now plot the temperature as a function of date.

Plot the time series of temperature with different colors for minimum and maximum temperature.

lincoln_minmax_weather |> 
  ...
lincoln_minmax_weather |> 
  ggplot(aes(x = date, y = temp, color = measure)) +
  geom_line()

Plotting x-y data: time series