parvin karimi parvin karimi - 1 year ago 34
R Question

How to change the default blue-color in `densCols` in R?

densCols
as part of the
grDevices
in R base by default gives a blue color determined by
blues9
. I was wondering if there might be a way we could change
blues9
to any different color while maintaining the
densCols
effect?

Notice, I need a single color.

As an example, how can we change the blue-color in the plot given by the following R code:

x = rnorm(1e4, 2, 1) ; y = rnorm(1e4, 1, 2)
plot(x, y, col = densCols(x, y, colramp = colorRampPalette(blues9[-(1:3)])) )

Answer Source

See ?colorRampPalette. You can pass any vector of colour names or values to the function. The default blues9[-(1:3)] just passes the last 6 elements of the 9-element vector of colour values, blues9.

For example, using the Greens palette from RColorBrewer:

library(RColorBrewer)
plot(x, y, col = densCols(x, y, colramp = colorRampPalette(brewer.pal(6, "Greens"))))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download