Joe*_*Joe 3 r anonymous-function ggplot2
我可以使用 调用scale_y_continuous() 中的匿名函数function(y) comma(y),但无法使用 ~ 约定调用匿名函数。这种情况下可以用~吗?
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))
Run Code Online (Sandbox Code Playgroud)
一个选项是换行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(.)))
Run Code Online (Sandbox Code Playgroud)
或者使用rlang::as_function(~ comma(.))
或者直接使用comma而不进行任何匿名函数调用
ggplot(mtcars[1:3,], aes(x = model, y = wt*2000)) +
geom_col() +
scale_y_continuous(labels = comma)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
261 次 |
| 最近记录: |