这是与中相同的问题
User defined colour palette in R and ggpairsis 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"
)

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

我想补充
scale_colour_manual(values=c('red','blue','green','red','blue'))

(显然,这只是伪代码),并且得到类似的信息(我并没有画出所有的点):

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

最佳答案

一种解决方案是从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

为了给

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

10-06 03:35