在scale_y_continuous中使用匿名函数 [英] using anonymous function within scale_y_continuous
本文介绍了在scale_y_continuous中使用匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 function(y)逗号(y)
在scale_y_continuous()中调用匿名函数,但不能使用〜约定来调用匿名函数.在这种情况下可以使用〜吗?
I can call an anonymous function in scale_y_continuous() using function(y) comma(y)
, but I cannot call an anonymous function using the ~ convention. Is it possible to use ~ in this situation?
library(scales)
library(ggplot2)
mtcars$model <- rownames(mtcars)
# Success
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = function(y) comma(y))
# Fail
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = ~comma(y))
推荐答案
一个选项是在 purrr :: as_mapper
library(scales)
library(ggplot2)
library(purrr)
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = as_mapper(~ comma(.)))
或使用 rlang :: as_function(〜逗号(.))
或者直接使用逗号
而不进行任何匿名函数调用
Or simply use comma
without any anonymous function call
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = comma)
这篇关于在scale_y_continuous中使用匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文