更新:
GGAlly再次更新,此答案中的黑客方法也不再有效。但最终有了非黑客解决方案:给出
scales <- scale_colour_brewer(type = 'qual') %+% scale_fill_brewer(type = 'qual')
您可以(以希望具有未来可靠性的方式)执行以下操作:
for (row in seq_len(ps$nrow))
for (col in seq_len(ps$ncol))
ps[row, col] <- ps[row, col] + scales
旧方法
另一个答案中的黑客方法已经不再适用,所以我们需要使用新的方法进行黑客攻击!
ggpairs
对象的内部结构是数据集和字符串列表:
> dta <- data.frame(a=1:6, b=7:12, c=c('f', 'g'))
> ps <- ggpairs(dta, 1:2, colour = 'c')
> str(ps)
List of 10
$ data :'data.frame': 2 obs. of 3 variables:
..$ a: int [1:2] 1 2
..$ b: int [1:2] 3 4
..$ c: int [1:2] 5 6
$ columns : int [1:3] 1 2 3
$ plots :List of 9
..$ : chr "ggally_densityDiag(ggally_data, ggplot2::aes(x = a, colour = c))"
..$ : chr "ggally_cor(ggally_data, ggplot2::aes(x = b, y = a, colour = c))"
[...]
$ gg : NULL
- attr(*, "class")= chr [1:2] "gg" "ggpairs"
> ps
![修改前的图表](https://istack.dev59.com/SmGdI.webp)
为了修改图表,需要修改图表对象中的相应字符串以包含附加命令。为此,我们使用deparse(substitute(argument))
来获取包含用户传递的代码的字符串,并将其附加到每个绘图调用中:
add_to_plots <- function(pairs, modification) {
str <- deparse(substitute(modification))
pairs$plots <- lapply(pairs$plots, function(s) paste(s, '+', str))
pairs
}
> add_to_plots(ps, scale_colour_brewer(type = 'qual'))
![plot after](https://istack.dev59.com/1sUym.webp)
NULL
,但那并不起作用。我不想用其他东西代替它 - 我希望它是透明的。 - Make42waiver()
,但似乎不起作用。我找不到添加aes(fill)
/scale_fill_*
的位置... - flying sheep