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 (NA
s) 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
Exercise instructions
- Fit a linear regression model using
Critic_Score
as the predictor variable andUser_Score
as the response variable. Store this model in the objectm
. - Create a scatterplot showing
Critic_Score
on the x-axis andUser_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 = ___)