这与问题相同 User defined colour palette in R and ggpairs或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'))
(显然这只是虚拟代码)并获得类似的东西(我没有绘制所有点):
答案 0 :(得分:11)
一种解决方案是从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
给予