3
votes

I'm working with a two column table that I am trying to make a density contour plot out of. "HiChIP_VillusvsCrypt" is the x axis and "RNAseq_VillusvsCrypt" is the y axis. If you see in this attached image, there is a legend with the density scaling from 0 to 0.2. I would like to be able to play around with this scaling, if possible (i.e. set bounds between 0 and 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))
1

1 Answers

3
votes

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.

Let's say we have the following 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)

If you want to set a multiplier on the underlying values, you can use the aes() function as follows:

aes(fill = after_stat(level * 10))

Note that the after_stat() function requires ggplot2 v3.3.0. Earlier versions use stat() or even older syntax is to use ..myvariable...