Automated faceting
In the previous exercise, you manually create a faceted scatterplot. This was not very tedious because you were only focused on two groups. However, there are 6 platforms in the vgsales2016
dataset, and it would be very tedious to manually code 6 scatterplots.
In this exercise, you will practice using group_by()
, nest()
, mutate()
, and map2()
to automate the process of creating a faceted scatterplot using the following template:
data %>%
group_by(factor) %>%
nest() %>%
mutate(
plot = map2(data, factor,
\(data, factor)
plot_ly(data = data, x = ~x, y = ~y) %>%
add_markers(name = ~factor)
)) %>%
subplot(nrows = R, shareY = TRUE, shareX = TRUE)
This exercise is part of the course
Interactive Data Visualization with plotly in R
Exercise instructions
- Use
group_by()
,nest()
, andmutate()
, andmap2()
to create a faceted scatterplot showingCritic_Score
on the x-axis andUser_Score
on the y-axis, where the facets are defined byPlatform
. - Arrange the facets in a grid with 3 rows.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a faceted scatterplot of User_Score vs. Critic_Score with 3 rows
vgsales2016 %>%
___(___) %>%
___() %>%
mutate(
plot = ___(
___, ___,
\(data, Platform)
)) %>%
subplot(nrows = ___, shareY = TRUE, shareX = TRUE)