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()
Trends
Trends involve fitting models to capture the general patterns in data while ignoring noise. We will visualize trends with the geom_smooth()
function, which by default uses the LOESS method of polynomial regression.
Here, we'll plot the trend in daily mean temperature.
lincoln_weather |>
ggplot(aes(x = date, y = mean_temp)) +
geom_point() +
geom_smooth()
Replicate the above plot but remove the error band.
lincoln_weather |>
...
lincoln_weather |>
ggplot(aes(x = date, y = mean_temp)) +
geom_point() +
geom_smooth(se = FALSE)
Wrap-up
Congratulations, you finished the tutorial!
To get credit for this assignment, replace my name with the first name that you submitted in the course introduction form in the code below and click Run Code to generate the text for you to submit to Canvas.
# replace my name below with your first name (surrounded by quotes)
first_name <- "Jeff"
generate_text(first_name)
Assignment complete!
Great! Copy that code into Canvas, and you're all set for this tutorial.