我想保存一些ggplot命令的规范供以后使用,因为我需要运行几个不同的图,这些图都具有一定的比例美感。
假设我想将其保存以供以后使用:
my.scale_aes <- scale_x_continuous(...) + scale_color_manual(...)
Run Code Online (Sandbox Code Playgroud)
当然,这将提示一条错误消息,表明您不能在没有直接ggplot()调用的情况下将ggproto对象一起添加。但是真的是这样吗?还有另一种方法可以将这些组件一起添加吗?
我在其他地方读到它与将元素添加在一起的不同方法有关:methods("+")我需要的东西与之有关,+.gg*但是我不知道如何实现它以及如何使其工作。
您可以通过定义所需ggplot术语的列表并将其添加进来。
library(ggplot2)
my.scale_aes <- list(
scale_x_continuous(breaks = c(56, 60, 61)),
scale_color_manual(values = c("black", "red"))
)
diamonds[1:100,] %>%
ggplot(aes(depth, price, color = cut == "Ideal")) +
geom_point() +
my.scale_aes
Run Code Online (Sandbox Code Playgroud)