gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:24:11 31 4
gpt4 key购买 nike

这是与在相同的问题
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"
)

enter image description here

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

(显然这只是虚拟代码)并得到类似的东西(我没有在所有的点上画):

enter image description here

最佳答案

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



enter image description here

关于r - 如何更改 GGally::ggpairs 的调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740210/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com