简体   繁体   English

如何修改 ggplot2 stat_density_2d 轮廓 plot 密度缩放?

[英]How to modify ggplot2 stat_density_2d contour plot density scaling?

I'm working with a two column table that I am trying to make a density contour plot out of.我正在使用一个两列表,我试图从中制作密度轮廓 plot 。 "HiChIP_VillusvsCrypt" is the x axis and "RNAseq_VillusvsCrypt" is the y axis. “HiChIP_VillusvsCrypt”是 x 轴,“RNAseq_VillusvsCrypt”是 y 轴。 If you see in this attached image, there is a legend with the density scaling from 0 to 0.2.如果您在此附加图像中看到,则有一个密度从 0 缩放到 0.2 的图例。 I would like to be able to play around with this scaling, if possible (ie set bounds between 0 and 0.1).如果可能的话,我希望能够使用这种缩放(即设置范围在 0 和 0.1 之间)。 I appreciate any advice!我很感激任何建议!

library(ggplot2)
df1 <- data.frame(HNF4_Looping_HiChIPvsRNASeq_VillusvsCrypt$HiChIP_log2FC_VillusvsCrypt, HNF4_Looping_HiChIPvsRNASeq_VillusvsCrypt$RNAseq_log2FC_VillusvsCrypt)
ggplot(df1, aes(df$HNF4_Looping_HiChIPvsRNASeq_VillusvsCrypt.HiChIP_log2FC_VillusvsCrypt, df$HNF4_Looping_HiChIPvsRNASeq_VillusvsCrypt.RNAseq_log2FC_VillusvsCrypt))+                       
  stat_density_2d(aes(fill = ..level.. ), geom = "polygon")+scale_x_continuous(name="HiChIP_VillusvsCrypt", limits=c(-4,4))+scale_y_continuous(name="RNASeq_VillusvsCrypt", limits=c(-4,4))

I can't reproduce your example code, so I'll substitute with a standard dataset to illustrate my points.我无法重现您的示例代码,因此我将替换为标准数据集来说明我的观点。

Playing with the bounds simply setting the limits on a colour/fill and making sure the oob argument is appropriate.使用边界只需设置颜色/填充的限制并确保oob参数是合适的。

Let's say we have the following plot.假设我们有以下 plot。

library(ggplot2)

myplot <- ggplot(faithful, aes(eruptions, waiting)) +
  stat_density_2d(aes(fill = after_stat(level)),
                  geom = "polygon") +
  xlim(1, 6) +
  ylim(35, 100)
myplot

We can play around with the limits as follows:我们可以玩弄如下限制:

myplot + scale_fill_continuous(limits = c(0, 0.01), 
                               oob = scales::squish)

Created on 2020-04-15 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 4 月 15 日创建

If you want to set a multiplier on the underlying values, you can use the aes() function as follows:如果要在基础值上设置乘数,可以使用aes() function,如下所示:

aes(fill = after_stat(level * 10))

Note that the after_stat() function requires ggplot2 v3.3.0.注意after_stat() function 需要 ggplot2 v3.3.0。 Earlier versions use stat() or even older syntax is to use ..myvariable.. .早期版本使用stat()甚至更旧的语法是使用..myvariable..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM