如何在函数中使用highcharter :: hchart?
hchart
这是一个使用该函数的简单折线图。
library(tidyverse)
library(lubridate)
library(highcharter)
library(nycflights13)
flights_2 <- flights %>%
mutate(dep_mo = ymd(str_c(year, month, "01", sep = "-"))) %>%
group_by(dep_mo) %>%
summarize(arr_delay = mean(arr_delay, na.rm = TRUE))
hchart(flights_2,
type = "line",
hcaes(x = dep_mo, y = arr_delay),
name = "Average Arrival Delay")
当我尝试编写一个函数来创建相同的图表时,我得到了一个错误。
h_fun <- function(df, x, y) {
hchart(df,
type = "line",
hcaes(x = x, y = y),
name = "Average Arrival Delay"
)
}
h_fun(df = flights_2, x = dep_mo, y = arr_delay)
这是错误消息:Error in mutate_impl(.data, dots) : Binding not found: x.
当我回溯错误时,它似乎hchart
正在使用dplyr::mutate_
. 这使我相信该错误与 NSE 有关,并且可能hchart
需要类似于ggplot2::aes_string()
(链接)的东西。但是,我在highcharter
.