1. Learn
  2. /
  3. Courses
  4. /
  5. Interactive Data Visualization with plotly in R

Connected

Exercise

Overlayed density plots

In this exercise, you will learn how to create density plots and overlay them to compare the distribution of critic scores for three video game publishers: Activision, Electronic Arts, and Nintendo.

To create a density plot for Critic_Score, store the results of the density() command, and then pass the x and y coordinates to add_lines():

d <- density(vgsales2016$Critic_Score, na.rm = TRUE)
plot_ly() %>%
  add_lines(x = ~d$x, y = ~d$y, fill = 'tozeroy') %>%
  layout(xaxis = list(title = 'Critic score'),
         yaxis = list(title = 'Density'))

Notice how you can create new plot types easily using familiar code! The fill = 'tozeroy' argument fills the area under the curve.

Data frames activision,ea, and nintendo are loaded, as is plotly.

Instructions

100 XP
  • Compute density curves of Critic_Score for Activision, EA, and Nintendo, storing them in the d.a, d.e, and d.n objects, respectively.
  • Create overlayed density plots of Critic_Score for activision, ea, and nintendo (in that order).