2016-01-12 4 views
4

Это тот же вопрос, как и в User defined colour palette in R and ggpairs или is there a way to change the color palette for GGally::ggpairs using ggplot?Как изменить цветовую палитру для GGally :: ggpairs?

только что решения там не работают.


Я также хочу, чтобы изменить цветовую палитру, но 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

+3

Пожалуйста, не настолько ленива и обеспечить по крайней мере MVe. –

+0

@Pascal: Нравится? – Make42

+0

@Pascal: Извините, забыли строку. Пожалуйста, попробуйте еще раз. – Make42

ответ

6

Одно из решений заключается в том, чтобы извлечь каждый участок из 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

Смежные вопросы