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.
Este ejercicio forma parte del curso
Interactive Data Visualization with plotly in R
Instrucciones del ejercicio
- Fit a linear regression model using
Critic_Scoreas the predictor variable andUser_Scoreas the response variable. Store this model in the objectm. - Create a scatterplot showing
Critic_Scoreon the x-axis andUser_Scoreon the y-axis. - Add a linear smoother to your scatterplot representing the fitted values.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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 = ___)