【发布时间】:2017-11-21 18:51:32
【问题描述】:
我可以很容易地为可以多次重复使用的因子水平设置一个参数,如下例所示:
am_labels_parameter <- c("auto", "manual")
d <-
mtcars %>%
mutate(
cyl = as.factor(cyl),
am = factor(am, labels = am_labels_parameter)
) %>%
group_by(cyl, am) %>%
summarise(mpg = mean(mpg))
我也可以使用 ggplot 刻度来执行此操作吗?我想设置 scale_y_continuous(scale_y_parameter) 以便可以在一系列绘图中轻松更新 scale_y_continuous 参数。
此代码有效:
d %>%
ggplot(aes(x = cyl, y = mpg, fill = am)) +
geom_col(position = "dodge") +
scale_y_continuous(
breaks = seq(0, 30, by = 10),
limits = c(0, 30)
)
这是我想工作的代码,但不知道如何设置参数:
scale_y_parameter <-
c(breaks = seq(0, 30, by = 10),
limits = c(0, 30))
d %>%
ggplot(aes(x = cyl, y = mpg, fill = am)) +
geom_col(position = "dodge") +
scale_y_continuous(scale_y_parameter)
非常感谢任何帮助。
【问题讨论】: