0

知道如何将字符串排序变量传递给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.

有什么建议么?

4

1 回答 1

1

啊解决了

mpg %>% mutate(manufacturer = fct_reorder(manufacturer, .[[order_var]]))
于 2019-01-31T10:09:35.760 回答