2014-03-06 45 views
5

我想在R中的GGally库中使用ggpairs的散点图的另一个调色板。请参阅similar question hereR和ggpairs中的用户自定义调色板

library(ggplot2) 
library(GGally) 

作品

ggplot(iris, aes(x=Sepal.Width, colour=Species)) + stat_ecdf() + scale_color_brewer(palette="Spectral") 

ggplot2_1

同样适用

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral") 
ggplot(iris, aes(x=Sepal.Width, colour=Species)) + stat_ecdf() 

ggplot2_2

不工作

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral") 

ggpairs(iris, 
    columns=, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), 
    colour='Species', 
    lower=list(continuous='points'), 
    axisLabels='none', 
    upper=list(continuous='blank') 
) 

ggpairs1 但加入

putPlot(p, ggplot(iris, aes(x=Sepal.Length, colour=Species)) + stat_ecdf(), 1,1) 

增加在正确的颜色的曲线。

ggpairs2

解决方法

我可以getPlot事后改变情节,但是这不是漂亮..

subplot <- getPlot(a, 2, 1) # retrieve the top left chart 
subplotNew <- subplot + scale_color_brewer(palette="Spectral") 
a <- putPlot(a, subplotNew, 2, 1) 

如何更改配色方案的散点图中ggpairs?更具体地说,我想手动定义像这样的颜色

scale_colour_manual(values=c("#FF0000","#000000", "#0000FF","#00FF00")) 

谢谢!

+0

嗨,你可以将'ggpairs'阴谋存储在一个对象中,比如'gg'并修改'gg $ plots' –

回答

3

这是一个可行的黑客:

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral") 
unlockBinding("ggplot",parent.env(asNamespace("GGally"))) 
assign("ggplot",ggplot,parent.env(asNamespace("GGally"))) 

当你分配一个新值ggplot功能,它是在全球环境。现在,GGally加载时会导入所有内容,包括ggplot(它不一定是这种方式)。此时,在全球环境中更改ggplot函数不起作用,因为从GGally导入优先。相反,您需要更新GGally:imports上的ggplot功能。只有一个问题:一旦一个包被加载,它的绑定被锁定。但是我们可以解锁它们(我猜这是不被接受的,因此将解决方案标记为黑客)。

查看Josh O'Brien在Replace definition of built-in function in R?下的答案以获取更多信息。

0

stacksia说(同时也加入了scale_fill_brewer)

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral") + scale_fill_brewer(palette="Spectral") 
unlockBinding("ggplot",parent.env(asNamespace("GGally"))) 
assign("ggplot",ggplot,parent.env(asNamespace("GGally"))) 

见乔什 - 奥布莱恩的回答Replace definition of built-in function in R?下获取更多信息。