Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color Gradients With ggplot

Tags:

r

colors

ggplot2

I have some geographic x,y,z data which I plot as follows

p<-ggplot(aes(x=x,y=y,fill=z),data=my_data)

I then apply a gradient

p<-p + scale_fill_gradient(low = "pink", high = "green")

And then tile and display the data

p<-p + geom_tile()
p

This works and gives the following:

enter image description here

One would think then, that I could replace the scale_fill_gradient() call with any number of other options such as

p<-p + scale_color_hue()
p<-p + scale_colour_gradientn(colours=c("#E5F5F9","#99D8C9","#2CA25F"))
p<-p + scale_colour_gradientn(colours = rainbow(7))
p<-p + scale_colour_brewer() 

would work.

But they don't, all I get is a gradient of blue.

Any thoughts as to why this might be, and how I can generate lots of happy colours?

like image 518
Richard Avatar asked Jul 03 '12 17:07

Richard


People also ask

How do you make a color scale in R?

If we want to create a color range in R, we can use the colorRampPalette function. In the previous R code, we have defined a new function called fun_color_range, which can be used to generate color ranges from the color with the color code #1b98e0 to red. The color codes are now stored in the data object my_colors.

How do you make a color scale?

Format cells by using color scales On the Home tab, click Conditional Formatting. Point to Color Scales, and then click the color scale format that you want. The top color represents larger values, the center color, if any, represents middle values, and the bottom color represents smaller values.

What are the default Ggplot colors?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.


1 Answers

There are two versions of these scale functions, one for colour and one for fill. You just needed to use the fill versions, i.e. scale_fill_hue, etc.

like image 92
joran Avatar answered Oct 03 '22 06:10

joran