0
votes

I have generated a ggplot2 graph and I want to fill it showing the density of the points. I have managed it using the following formula for density:

get_density <- function(x, y, n = 250) {
  dens <- MASS::kde2d(x = x, y = y, n = n)
  ix <- findInterval(x, dens$x)
  iy <- findInterval(y, dens$y)
  ii <- cbind(ix, iy)
  return(dens$z[ii])}

and then getting the density of the points in a new "density" column based on the formula, with "lfc" and "pval" being the x,y variables:

data.MA$density <- get_density(data.MA$pval, data.MA$lfc)

The ggplot object I am plotting is:

  heatmap2 <- ggplot() + 
  geom_point(data = filter(data.MA, chg == "unchanged"), 
             aes(basemean, lfc, color = density)) + 
  geom_point(data = filter(data.MA, chg == "changed"), 
             aes(basemean, lfc, fill = dir), 
             shape = 21, size = 2, stroke = 0.1) +
  scale_fill_manual(values = c("#FFA600", "#00B2FF", "#00B2FF")) +
  scale_colour_gradient2(low = "blue", mid = "white", high = "red", midpoint = 10) +
  theme_bw() + scale_y_continuous() + scale_x_continuous()

This gives me the following graph:

MA plot gradient

Here is a samples of the data.MA:

gene     pval  lfc basemean      density       dir peakid     chg
1    NXT1 41.22403 3.58     9.50 3.339690e-02 increased      1 changed
2   BEND5 23.41567 5.03     8.01 0.000000e+00 increased      2 changed
3    PLB1 23.19450 7.91     8.13 4.849746e-78 increased      3 changed
4   LYRM9 20.81531 2.35    11.43 0.000000e+00 increased      4 changed
5 MIR4464 14.73049 3.65     7.99 0.000000e+00 increased      5 changed
6 HSD17B2 14.63451 4.51     7.31 0.000000e+00 increased      6 changed

My question is how can I make the gradient more disperse in the middle, so it is not just a red stripe in the middle and more of a gradient?

Can anyone suggest a different formula for the density perhaps?

Or a way to define the gradient better to be in more separate blocks?

1
Could you provide a reproducible example? data.MA is missing - Edgar Santos
An option here is to use a transformation in the y axis e.g. scale_y_sqrt() - Edgar Santos
@ed_sans, thanks, I have now provided a sample of the data.MA, the problem with scaling the axis as suggested is the values above and below zero are not affected symmetrically. - reubenmcg
Try using dput(data.MA), then copy/paste that output into your question to help provide sample data. - www
@Ryan Runge, the problem is I have over 60,000 rows, what would be the best way in the future to convey the data so it can be reproduced? - reubenmcg

1 Answers

1
votes

the data sample is too small to try to reproduce the plot and check for possible solutions but here is an attempt using scale_colour_gradientn and emphasizing the midpoints

#emulating your solution:

df = data.frame(seq = 1:1000, rnorm= rnorm(1000)+5)

library(ggplot2)
A = ggplot(df) + 
      geom_point(aes(seq, rnorm, color = rnorm)) + 
      scale_colour_gradient2(low = "blue", mid = "white", high = "red", midpoint = 5) +
      theme_bw() + scale_y_continuous() + scale_x_continuous()

#providing a function for making gradients
colfunc = colorRampPalette(c("blue", "white", "red"))
#providing an exponential gradient
exp_seq= seq(from = 0.1, to = 0.6, length.out = 4)^2
B = ggplot(df) + 
      geom_point(aes(seq, rnorm, color = rnorm)) + 
      scale_colour_gradientn(colors=colfunc(11), values = c(0, exp_seq, rev(1-exp_seq), 1)) +
      theme_bw() + scale_y_continuous() + scale_x_continuous() 

library(cowplot)
plot_grid(A, B, ncol = 2)

enter image description here

EDIT

The argument values of scale_colour_gradientn function accepts a vector of values ranging from 0 - 1. There should be as many elements in this vector as there are colors.

colfunc(11) is a function that makes a vector of colors, in this case it will output 11 colors since the argument is specified as 11. So the argument values of scale_colour_gradientn needs to have 11 values on which it will map the colors. These values relate to your data by a function scales::rescale (https://www.rdocumentation.org/packages/scales/versions/0.4.1/topics/rescale) which takes the range of your data and maps it to 0 - 1 range. If the range of your data is for instance 100 - 400 and you want a midpoint at 153, than:

scales::rescale(c(100, 153, 400)) 
#output 0.0000000 0.1766667 1.0000000

You would want to map "white" color (element 6 of the vector specified for colors argument in the above example) color at 0.1766667, apart from that you will need to specify additional 4 elements in the range 0 - 0.1766667 for light blue colors and an additional 4 elements for light red colors. So for example

values = c(0, 0.01, 0.03, 0.6, 0.1, 0.1766667, 0.3, 0.5, 0.7, 0.9, 1) but that is a bit arbitrary, perhaps a better strategy is the check your data and see in what range you would like to emphasize and chose those values. For instance:

range of data is 100 to 400, you want midpoint at 153 and to emphasize 123 - 183, you could provide a custom range for values argument:

scales::rescale(c(100, 113, 123, 133, 143, 153, 163, 173, 183, 193, 400))
#output 0.00000000 0.04333333 0.07666667 0.11000000 0.14333333 0.17666667 0.21000000 0.24333333 0.27666667 0.31000000 1.00000000

In the exp_seq part I just defined a vector exp_seq= seq(from = 0.1, to = 0.6, length.out = 4)^2

exp_seq
#output 0.01000000 0.07111111 0.18777778 0.36000000

rev(exp_seq) #just the reverse
#output 0.36000000 0.18777778 0.07111111 0.01000000

and included those in a another vector

c(0, exp_seq, 1-rev(exp_seq), 1)
#output 0.00000000 0.01000000 0.07111111 0.18777778 0.36000000 0.64000000 0.81222222 0.92888889 0.99000000 1.00000000

and passed that to values argument to map the 11 colors I defined in the colors argument. If I was to do:

ggplot(df) + 
  geom_point(aes(seq, rnorm, color = rnorm)) + 
  scale_colour_gradientn(colors=colfunc(11), values = scales::rescale(c(100, 113, 123, 133, 143, 153, 163, 173, 183, 193, 400))) +
  theme_bw() + scale_y_continuous() + scale_x_continuous() 

the result would look like:

enter image description here

If anything is unclear please let me know.