将字符串变量传递给 forcats::fct_reorder

Passing string variable to forcats::fct_reorder

知道如何将字符串排序变量传递给 fct_reorder 吗?

require(dplyr)
require(forcats)
require(ggplot2)

order_var = 'displ'

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, order_var))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

尝试过 bang bang !!

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, !!order_var))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

尝试过 eval:

as.name(eval(order_var))
#> displ

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, as.name(eval(order_var))))
#> Error in mutate_impl(.data, dots): Evaluation error: length(f) == length(.x) is not TRUE.

有什么建议吗?

啊解决了

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, .[[order_var]]))