Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set fixed continuous colour values in ggplot2

Tags:

I'm plotting a lot of graphics and I'd like for all of them to have the same colour scale so I can compare one to another. Here's my code:

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) print(ggplot(mydata, aes(x= X, y= Y, colour= Z)) + geom_point(alpha=.5,size = 6) + scale_colour_gradientn(colours = myPalette(100)) + ylim(.1,.4) + xlim(1.5,2) + ggtitle(title)) 

Is there a way to set this colour scale?

like image 799
Lucien S. Avatar asked Feb 03 '14 21:02

Lucien S.


People also ask

How do I specify colors in ggplot2?

When creating graphs with the ggplot2 R package, colors can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”). It is also possible to use pre-made color palettes available in different R packages, such as: viridis, RColorBrewer and ggsci packages.

What are the default Ggplot Colours?

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

What are the different types of color scales?

There are three main types of colour schemes: sequential, diverging, and qualitative and they each are best used to describe different types of data. We will now explore these colour schemes with some interactive visualizations.

Which package can you use for extra themes and color scales for ggplot2?

The ggthemr package was developed by a friend of mine, Ciarán Tobin, who works with me at KillBiller and Edgetier. The package gives a quick and easy way to completely change the look and feel of your ggplot2 figures, as well as quickly create a theme based on your own, or your company's, colour palette.


1 Answers

Do I understand this correctly? You have two plots, where the values of the color scale are being mapped to different colors on different plots because the plots don't have the same values in them.

library("ggplot2") library("RColorBrewer") ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) +    geom_point(size=6) 

enter image description here

ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) +    geom_point(size=6) 

enter image description here

In the top one, dark blue is 1 and light blue is 4, while in the bottom one, dark blue is (still) 1, but light blue is now 8.

You can fix the ends of the color bar by giving a limits argument to the scale; it should cover the whole range that the data can take in any of the plots. Also, you can assign this scale to a variable and add that to all the plots (to reduce redundant code so that the definition is only in one place and not in every plot).

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) sc <- scale_colour_gradientn(colours = myPalette(100), limits=c(1, 8))  ggplot(subset(mtcars, am==0), aes(x=wt, y=mpg, colour=carb)) +    geom_point(size=6) + sc 

enter image description here

ggplot(subset(mtcars, am==1), aes(x=wt, y=mpg, colour=carb)) +    geom_point(size=6) + sc 

enter image description here

like image 190
Brian Diggs Avatar answered Oct 28 '22 16:10

Brian Diggs