使用两个缩放颜色渐变ggplot2 [英] Using two scale colour gradients ggplot2

查看:117
本文介绍了使用两个缩放颜色渐变ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有的话,我认为对此应该有一个非常简单的解决方案。我有两个大型的数据框,基本上是这样的:

 > data1 [1,] 
染色体起始末端测试参考位置log2 p.value
13600 Y 10199251 10200750 533 616 10200000 0.2181711 0.00175895
...

> data2 [1,]
染色体起始末端测试参考位置log2 p.value
4080 Y 10197501 10202500 403 367 10200000 0.04113596 0.3149926
...



我使用此代码将两个数据框绘制到同一个图中:

<$ p $ (x =位置,y = 1,...,y),其中p = log2,color = p.value))
+ geom_point(data = subset(data2,p.value> = glim [1]),map = aes(x = position,
y = log2,color = p.value))

当我绘制单个数据框时,我使用的是红白色p.value列中的值的渐变。使用此行:

  p < -  p + scale_colour_gradient(限制= glim,trans ='log10',low =red ,
high =white)

中心问题是:现在有两个数据框,我怎样才能为data1设置一个颜色梯度,为data2设置另一个颜色梯度?我在之前的文章中读到,不可能使用两种不同的色标(例如,第一个是low =,第二个是high =),但在这种情况下是完全相同的色标(如果我不混淆术语)。语法显然是不正确的,但我想要做这样的事情:

  p < -  p + scale_colour_gradient(limits = glim,trans ='log10',low =red,
high =white)

p <-p + scale_colour_gradient(限制= glim,trans ='log10',低=蓝,
高=白)


解决方案<首先,请注意,ggplot不鼓励这一点的原因是因为这些图很难解释。

通过诉诸一下作弊,你可以得到你的两个颜色渐变尺度。在geom_point中,某些形状(21到25)可以同时具有填充和颜色。您可以利用它创建一个填充比例的图层,另一个图层用彩色比例。

 #dummy up数据
dat1 <-data.frame(log2 = rnorm(50),p.value = runif(50))
dat2 <-data.frame(log2 = rnorm(50),p.value = runif(50))

#带两个比例的geom_point
p < - ggplot()
p < - p + geom_point(data = dat1,aes(x = p.value ,y = log2,color = p.value),shape = 21,size = 3)
p <-p + scale_color_gradient(low =red,high =gray50)
p < p + geom_point(data = dat2,aes(x = p.value,y = log2,shape = shp,fill = p.value),shape = 21,size = 2)
p <-p + scale_fill_gradient low =gray90,high =blue)


If any, I think there must be a very easy solution for this. I have two large dataframes that basically look like these:

> data1[1,]
      chromosome start    end      test ref position log2      p.value 
13600 Y          10199251 10200750 533  616 10200000 0.2181711 0.00175895   
...

> data2[1,]
      chromosome start    end      test ref position log2       p.value 
4080  Y          10197501 10202500 403  367 10200000 0.04113596 0.3149926   
...

I'm using this code to plot the two dataframes into the same graph:

p <- ggplot() + geom_point(data=subset(data1, p.value >= glim[1]),
map=aes(x=position, y=log2, colour=p.value))
+ geom_point(data=subset(data2, p.value >= glim[1]), map=aes(x=position,
y=log2, colour=p.value))

When I was plotting single dataframes, I was using a red-white color gradient for the values in "p.value" column. Using this line:

p <- p + scale_colour_gradient(limits=glim, trans='log10', low="red", 
high="white") 

The central issue is: Now with two dataframes, how can I set one color gradient for data1 and another for data2? I read in a previous post that it is not possible to use two different colour scales(ej. "low=" for the first, and "high=" for the second), but in this case is exactly the same kind of colour scale (If I'm not mixing up terminology). The syntax obviously is not correct but I'd like to do something like this:

p <- p + scale_colour_gradient(limits=glim, trans='log10', low="red", 
high="white") 

p <- p + scale_colour_gradient(limits=glim, trans='log10', low="blue", 
high="white") 

解决方案

First, note that the reason ggplot doesn't encourage this is because the plots tend to be difficult to interpret.

You can get your two color gradient scales, by resorting to a bit of a cheat. In geom_point certain shapes (21 to 25) can have both a fill and a color. You can exploit that to create one layer with a "fill" scale and another with a "color" scale.

# dummy up data
dat1<-data.frame(log2=rnorm(50), p.value= runif(50))
dat2<-data.frame(log2=rnorm(50), p.value= runif(50))

# geom_point with two scales
p <- ggplot() 
p <- p + geom_point(data=dat1, aes(x=p.value, y=log2, color=p.value), shape=21, size=3)
p <- p + scale_color_gradient(low="red", high="gray50")
p <- p +  geom_point(data= dat2, aes(x=p.value, y=log2, shape=shp, fill=p.value), shape=21, size=2)
p <- p + scale_fill_gradient(low="gray90", high="blue")

这篇关于使用两个缩放颜色渐变ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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