I have a data frame like this
BP R2 LOG10
96162057 0.2118000 2.66514431
96162096 0.0124700 0.31749391
96162281 0.0008941 0.07012148
96163560 0.5011000 2.48505399
96163638 0.8702000 3.37778598
and I want to plot BP against LOG10, and color the points by R2. R2 are continuous values from 0-1.
myplot <- read.cvs("mytable.csv",head=TRUE)
attach(myplot)
ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point()
So far, so good. However I would like to display the R2 colors in manually selected intervals and colors, like this (if I had discrete values).
ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point() +
scale_color_manual(breaks= c("1","0.8","0.6","0.4","0.2","0"),
values = c("red","yellow","green","lightblue","darkblue"))
Error: Continuous value supplied to discrete scale
This looks pretty, but I would rather set the colors my self.
ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point(shape=1) +
scale_colour_gradientn(colours = rainbow(10))
So, how can I manually select intervals from continuous values (1-0.8, 0.8-0.6, 0.6-0.4, 0.4-0.2, 0.2-0), and color them to my liking (red, yellow, green, light, darkblue)? A smooth gradient between the colors would be cool, but not crucial.
Change ggplot colors by assigning a single color value to the geometry functions ( geom_point , geom_bar , geom_line , etc). You can use R color names or hex color codes. Set a ggplot color by groups (i.e. by a factor variable). This is done by mapping a grouping variable to the color or to the fill arguments.
Key functions to change gradient colorsscale_color_gradient() , scale_fill_gradient() for sequential gradients between two colors. scale_color_gradient2() , scale_fill_gradient2() for diverging gradients. scale_color_gradientn() , scale_fill_gradientn() for gradient between n colors.
In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.
The default ggplot2 setting for gradient colors is a continuous blue color. In the following example, we color points according to the variable: Sepal.Length. sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width))+
# For diverging colour scales use gradient2ggplot(df, aes(x, y)) + geom_point(aes(colour=z1)) + scale_colour_gradient2() # Use your own colour scale with gradientnggplot(df, aes(x, y)) + geom_point(aes(colour=z1)) + scale_colour_gradientn(colours=terrain.colors(10))
Note that, the functions scale_color_continuous () and scale_fill_continuous () can be also used to set gradient colors. In the example below, we’ll use the R base function rainbow () to generate a vector of 5 colors, which will be used to set the gradient colors.
You can use scale_colour_gradientn () and then provide your own colours= and values=. Values will give intervals for each color. ggplot (myplot,aes (BP,LOG10, color = R2)) + geom_point () + scale_colour_gradientn (colours = c ("red","yellow","green","lightblue","darkblue"), values = c (1.0,0.8,0.6,0.4,0.2,0))
You can use scale_colour_gradientn()
and then provide your own colours=
and values=
. Values will give intervals for each color.
ggplot(myplot,aes(BP,LOG10, color = R2)) + geom_point() +
scale_colour_gradientn(colours = c("red","yellow","green","lightblue","darkblue"),
values = c(1.0,0.8,0.6,0.4,0.2,0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With