Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I often work with ggplot2 that makes gradients nice (click here for an example). I have a need to work in the base and I think scales can be used there to create color gradients as well but I'm severely off the mark on how. The basic goal is to generate a palette of n colors that range from x color to y color. The solution needs to work in the base though. This was a starting point but there's no place to input an n.

 scale_colour_gradientn(colours=c("red", "blue"))

I am well aware of:

brewer.pal(8, "Spectral") 

from RColorBrewer. I'm looking more for the approach similar to how ggplot2 handles gradients that says I have these two colors and I want 15 colors along the way. How can I do that?

1 Answer

0 votes
by

To generate a gradient of n colors ranging from color 1 to color 2, you can use the colorRampPalette function, as follows:

colfun <- colorRampPalette(c("red", "blue"))

plot(rep(1,10),col=colfun(10),pch=20,cex=5)

Output:

image

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories

...