I am looking at a dataset from tidytuesday, available here:
video_games <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-07-30/video_games.csv")
I wrote this code to create a horizontal bar plot, ranked in descending order.
video_games %>%
top_n(10, metascore) %>%
arrange(desc(metascore)) %>%
plot_ly(x = ~metascore, y = ~fct_reorder(game, metascore),
type = "bar") %>%
layout(xaxis = list(title = "Metascore"),
yaxis = list(title = ""))
I want to reuse the code with multiple variables without copying and pasting, so I created a function with 2 entries for the variables I want to plot. (I left out the layout section. If there is a way to automatically re-lable the plot inside the function, that would be cool.)
video_games_ranking_plot <- function(A, B) {
top_n(10, A) %>%
arrange(desc(A)) %>%
plot_ly(x = ~A, y = ~fct_reorder(B, A),
type = "bar")
}
When I run the function
video_games %>%
video_games_ranking_plot(metascore, game)
... I get the error message Error in video_games_ranking_plot(., metascore, game) :
unused argument (game)
Does anyone know why?
