【问题标题】:How to change the color palette for GGally::ggpairs?如何更改 GGally::ggpairs 的调色板?
【发布时间】:2016-01-12 09:46:36
【问题描述】:

这是同一个问题 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'))

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

【问题讨论】:

  • 请不要那么懒惰,至少提供一个 MVE。
  • @Pascal:抱歉,忘记了一行。请重试。
  • 您的预期输出是什么?因为我用你的代码得到了彩色数字。
  • @Pascal:如果你运行你在 MWE 中看到的代码,你会得到第一个图。我相信你也得到了那个情节,对吧?这里的颜色是红色,有些沙黄色,绿色,蓝色,洋红色,对吧?我想改变这些颜色。例如,我不在乎哪种颜色。一个例子是“红色”、“蓝色”、“绿色”、“红色”、“蓝色”。我不确定这个例子在哪里不清楚。
  • 这将不是这样做的方式,但这是一种方式 ...只需覆盖矩阵中的每个图... for(i in 1:2) { for(j in 1:2){ p[i,j] &lt;- p[i,j] + scale_fill_manual(values=c("red", "blue", "green", "yellow", "black")) + scale_color_manual(values=c("red", "blue", "green", "yellow", "black")) } }

标签: r ggally


【解决方案1】:

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

给予

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2015-05-06
    • 2015-04-10
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多