Get startedGet started for free

Adding a linear smoother

You've seen how to add LOESS smoothers to a scatterplot by using both the add_markers() and add_lines() traces. Adding a linear smoother uses the same approach, but you use lm() command to fit the linear model.

In this exercise, your task is to add a linear smoother to a scatterplot of user score against critic score for video games in 2016.

When you add smoothers, missing values (NAs) can be problematic because many modeling functions automatically delete missing observations. To avoid this conflict, use select() and na.omit() to delete observations before plotting.

Note that plotly and the vgsales2016 data has already been loaded for you.

This exercise is part of the course

Interactive Data Visualization with plotly in R

View Course

Exercise instructions

  • Fit a linear regression model using Critic_Score as the predictor variable and User_Score as the response variable. Store this model in the object m.
  • Create a scatterplot showing Critic_Score on the x-axis and User_Score on the y-axis.
  • Add a linear smoother to your scatterplot representing the fitted values.

Hands-on interactive exercise

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

# Fit the regression model of User_Score on Critic_Score
m <- lm(___ ~ ___, data = ___)

# Create the scatterplot with smoother
vgsales2016 %>%
   select(User_Score, Critic_Score) %>%
   na.omit() %>%
   ___(x = ___, y = ___) %>%
   ___(showlegend = FALSE) %>%
   ___(y = ___)
Edit and Run Code