geom_tile不同因素的不同渐变比例和颜色 [英] geom_tile different gradient scale and color for different factors

查看:198
本文介绍了geom_tile不同因素的不同渐变比例和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制以下数据框.

Hi I would like to plot the following dataframe.

d<- data.frame (pid=c("d","b","c"), type=c("rna","rna","rna"), value = c(1,2,3) )
d2 <- data.frame (pid=c("d","b","c"), type=c("dna","dna","dna"), value = c(10,20,30) )
df <- rbind (d,d2)

 ggplot(df, aes(y=pid, x=type  ) ) + geom_tile(aes(fill = value),
  colour = "white") + scale_fill_gradient(low = "white",
  high = "steelblue") 

这将产生一个如下所示的情节,

This produces a plot that looks like this,

但是,我希望每个x因子都有自己的颜色渐变,因此理想情况下rna是蓝色到白色,而dna是红色到白色.反正有这样做吗?如果不可能使用不同的梯度,那么不同的比例呢?谢谢!

however, I would like to have each x factor have its own color gradient, so ideally rna is blue to white while dna is red to white. Is there anyway to do this? Of if different gradient is not possible, then what about just different scales? thanks!

推荐答案

这是将@Brian的建议应用于您的原始示例的样子.您可能需要分别重新缩放rnadna值,以使颜色范围更具可比性.

Here is what it looks like applying @Brian's suggestion to your original example. You may want to rescale the rna and dna value separately, to make the color ranges more comparable.

p = ggplot(df, aes(y=pid, x=type, fill=type, alpha=value)) +
    geom_tile(colour="white", size=1) + 
    scale_fill_manual(values=c(dna="salmon", rna="steelblue")) +
    theme_bw() +
    theme(panel.grid=element_blank()) +
    coord_cartesian(expand=FALSE)

这篇关于geom_tile不同因素的不同渐变比例和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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