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

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

问题描述

如果有的话,我认为必须有一个非常简单的解决方案.我有两个大数据框,基本上如下所示:

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

当我绘制单个数据框时,我对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") 

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

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

推荐答案

首先,请注意 ggplot 不鼓励这样做的原因是因为情节往往难以解释.

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

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

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() +
       geom_point(data=dat1, aes(x=p.value, y=log2, color=p.value), shape=21, size=3) +
       scale_color_gradient(low="red", high="gray50") +
       geom_point(data= dat2, aes(x=p.value, y=log2, shape=shp, fill=p.value), shape=21, size=2) +
       scale_fill_gradient(low="gray90", high="blue")
p

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

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