如何更改 GGally::ggpairs 的调色板?

How to change the color palette for GGally::ggpairs?

这是同一个问题 User defined colour palette in R and ggpairs or is there a way to change the color palette for GGally::ggpairs using ggplot?

只是那里的解决方案不再有效。


我也想更改调色板,但是is there a way to change the color palette for GGally::ggpairs using ggplot? 不起作用了。怎么办?

MWE:

library(GGally)
library(ggplot2)
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
ggpairs(
  diamonds.samp[,1:2],
  mapping = ggplot2::aes(color = cut),
  upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
  lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
  diag = list(continuous = wrap("densityDiag")),
  title = "Diamonds"
)

我要补充

scale_colour_manual(values=c('red','blue','green','red','blue'))

(显然那只是伪代码)并得到类似的东西(我没有画满所有的点):

一个解决方案是从 ggmatrix 中提取每个图,添加一个新的 scale_,然后将其重新分配回矩阵。

例子

library(GGally)
library(ggplot2)
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]

p <- ggpairs(
  diamonds.samp[,1:2],
  mapping = ggplot2::aes(color = cut),
  upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
  lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
  diag = list(continuous = wrap("densityDiag")),
  title = "Diamonds"
)

遍历每个图改变相关尺度

for(i in 1:p$nrow) {
  for(j in 1:p$ncol){
    p[i,j] <- p[i,j] + 
        scale_fill_manual(values=c("red", "blue", "green", "yellow", "black")) +
        scale_color_manual(values=c("red", "blue", "green", "yellow", "black"))  
  }
}

p

给予