R和ggpairs中用户定义的调色板

byt*_*ght 5 r ggplot2

我试图使用从散点图另一个调色板ggpairs来自GGally于R.库见这里类似的问题.

library(ggplot2)
library(GGally)
Run Code Online (Sandbox Code Playgroud)

作品

ggplot(iris, aes(x=Sepal.Width, colour=Species)) + stat_ecdf() + scale_color_brewer(palette="Spectral")
Run Code Online (Sandbox Code Playgroud)

ggplot2_1

也有效

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

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')
)
Run Code Online (Sandbox Code Playgroud)

ggpairs1 但补充一点

putPlot(p, ggplot(iris, aes(x=Sepal.Length, colour=Species)) + stat_ecdf(), 1,1)
Run Code Online (Sandbox Code Playgroud)

添加正确颜色的图表.

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)
Run Code Online (Sandbox Code Playgroud)

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

scale_colour_manual(values=c("#FF0000","#000000", "#0000FF","#00FF00"))
Run Code Online (Sandbox Code Playgroud)

谢谢!

sta*_*sia 3

这是一个有效的技巧:

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

当您为函数分配新值时ggplot,它位于全局环境中。现在,GGally导入所有内容,包括ggplot加载时间(不必如此)。此时,更改ggplot全局环境中的函数不会产生任何影响,因为 from 的导入GGally具有优先权。相反,您需要ggplot更新GGally:imports. 只有一个问题:一旦加载了一个包,它的绑定就会被锁定。但我们可以解锁它们(我猜这是不受欢迎的,因此将解决方案标记为黑客)。

请参阅 Josh O'Brien在替换 R 中内置函数的定义下的回答?了解更多信息。