ggplot手动设置scale_color_gradientn [英] ggplot set scale_color_gradientn manually

查看:4944
本文介绍了ggplot手动设置scale_color_gradientn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框

  BP R2 LOG10 
96162057 0.2118000 2.66514431
96162096 0.0124700 0.31749391
96162281 0.000 b $ b 96162281 0.0008941 0.07012148
96163560 0.5011000 2.48505399
96163638 0.8702000 3.37778598

并且我想绘制BP对照LOG10,并且颜色的点由R2。 R2是0-1的连续值。

  myplot<  -  read.cvs(mytable.csv,head = TRUE)
attach(myplot)
ggplot(myplot,aes(BP,LOG10,color = R2))+ geom_point()

到目前为止,这么好。但是,我想以手动选择的间隔和颜色显示R2颜色,像这样(如果我有离散值)。

  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))
错误:连续值提供给离散比例

这看起来很漂亮,但我宁愿设置我自己的颜色。

  ggplot(myplot,aes(BP,LOG10,color = R2))+ geom_point(shape = 1)+ 
scale_colour_gradientn因此,我如何从连续值(1-0.8,0.8-0.6,0.6-0.6)手动选择间隔, 0.4,0.4-0.2,0.2-0),并按他们喜欢的颜色(红,黄,绿,光,暗蓝)?颜色之间的平滑渐变会很酷,但不是至关重要。

解决方案

您可以使用 scale_colour_gradientn() code> colors = values = 。值将为每种颜色提供间隔。

  ggplot(myplot,aes(BP,LOG10,color = R2))+ geom_point )+ 
scale_colour_gradientn(colors = c(red,yellow,green,lightblue,darkblue),
values = c(1.0,0.8,0.6,0.4,0.2 ,0))


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.

解决方案

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)) 

这篇关于ggplot手动设置scale_color_gradientn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆