3
library(ggplot2)

这段代码产生了一个漂亮的情节:

qplot(cty, hwy, data = mpg, colour = displ) +
scale_y_log2() + 
labs(x="x axis") + 
labs(y="y axis") +
opts(title = "my title")

但我想设置变量来尝试减少代码重复:

log_scale <- scale_y_log2()
xscale <-   labs(x="x axis")
yscale <-   labs(y="y axis") 
title <- opts(title = "my title")
my_scales <- c(log_scale, xscale, yscale, title) 
# make a variable to hold the scale info changes above

这样我就可以做到这一点并同时添加一堆东西:

qplot(cty, hwy, data = mpg, colour = displ) + my_scales  
# add these to your plot.   

但我收到此错误:

Error in object$class : $ operator is invalid for atomic vectors

我意识到进入 my_scales 的东西需要是层/不同类型的对象,但我看不出它们应该是什么。

4

1 回答 1

4

使用列表:

my_scales <- list(log_scale, xscale, yscale, title) 
于 2010-04-12T01:35:36.857 回答