MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Interactive Data Visualization with plotly in R

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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 dan Jalankan Kode