Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User defined colour palette in R and ggpairs

Tags:

r

ggplot2

I am trying to use another color palette for the scatter plots from ggpairs from the GGally library in R. See similar question here.

library(ggplot2)
library(GGally)

Works

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

ggplot2_1

Also works

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

ggplot2_2

Does not work

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 but adding

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

adds a plot in the right colors.

ggpairs2

Workaround

I can change the plots afterwards with getPlot, but that's not pretty..

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

How can I change the color scheme for the scatter plots in ggpairs? More specifically, I'd like to manually define the colors like so

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

Thanks!

like image 977
bytesinflight Avatar asked Mar 06 '14 21:03

bytesinflight


People also ask

How do you set a color palette in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

What is Ggpairs R?

The ggpairs() function from the GGally package allows us to build a great scatterplot matrix. Scatterplots of each pair visualized in left side of the plot and Pearson correlation value and significance displayed on the right side.

How do I specify colors in ggplot2?

A color can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”).


1 Answers

Here is a hack that works:

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

When you assign a new value to the ggplot function, it is in the global environment. Now, GGally imports everything including ggplot when it loads (it didn't have to be that way). At that point, changing the ggplot function in your global environment has no effect, because imports from GGally have precedence. Instead, you need to update the ggplot function on the GGally:imports. There is only one problem: once a package is loaded, its bindings are locked. But we can unlock them (I am guessing this is frowned upon, hence labeling the solution a hack).

See Josh O'Brien's answer under Replace definition of built-in function in R? for more info.

like image 186
stacksia Avatar answered Oct 11 '22 15:10

stacksia