r - How to set fixed continuous colour values in ggplot2 -


i'm plotting lot of graphics , i'd of them have same colour scale can compare 1 another. here's 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 way set colour scale?

do understand correctly? have 2 plots, values of color scale being mapped different colors on different plots because plots don't have 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 top one, dark blue 1 , light blue 4, while in bottom one, dark blue (still) 1, light blue 8.

you can fix ends of color bar giving limits argument scale; should cover whole range data can take in of plots. also, can assign scale variable , add plots (to reduce redundant code definition in 1 place , 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


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

Git submodule update: reference is not a tree... but commit IS there -