0

I've created a heat map in ggplot and I'm using a default red to white scale_fill_gradient. When I plot the gradient however, it appears to have an orange tint. I was wondering if there was a way to edit how ggplot calculates the gradient so that I get pink intermediates so that it matches the other figures I'm making. Related to this graph, though I can post separately, I want to make my NA values have a striped pattern (rather than grey fill). I haven't found a way to do that in ggplot.

    p <- ggplot(heat.data, aes(x = MutResFac, y = mutAA, fill = disruption)) +
            geom_tile(color="grey50") +
            scale_fill_gradient(name = "Disruption", limits = c(0,3),
                                 labels = c("WT-like", "Mild", "Moderate", "Severe"),
                                 low = "#FFFFFF", high = "#FF0000",
                                 guide = "legend") +
            theme(axis.text.x = element_text(size = 25, hjust = 1), 
                  text = element_text(size = 25), 
                  plot.title = element_text(hjust = 0.5)) + 
            coord_fixed() +
            labs(x = "Position", y = "Mutant Amino Acid") +
            ggtitle(title) + 
            scale_x_discrete(labels = wildTypeSeqLabels) 

A example plot of the data

Cfun
  • 8,442
  • 4
  • 30
  • 62

1 Answers1

0

The simple answer as to why your middle range of colors from red to white comes out orange is that he in-between color value from #FF0000 and white is orange. The better practical color theory answer is that #FF0000 is a yellowish or orange-ish red, and not a blueish red, so the middle color will be orangeish. The most technical answer is explained in the technical documentation for the gradient color scale functions. The scale_..._gradient functions create the gradient based on keeping hue constant and varying chroma and luminance.

Options to fix scale

For this, I'll use an example dataset and plot with the base code here:

set.seed(1111)
df <- data.frame(x=1:10, y=rnorm(10,1,0.2), values=1:10)
p <- ggplot(df, aes(x,y, fill=values)) + geom_col(width=0.7)

You can consider one of the following:

  1. select a more blue-ish red to start with:

    p + scale_fill_gradient(low='#FFFFFF', high='#FE43AF')

enter image description here

  1. Select your scale more discretely using scale_fill_gradientn:

    pal <- c('#F8F6F6', '#F4AABC', '#E83D7D') p + scale_fill_gradientn(colours = pal)

enter image description here

Incidentally, the function choose_color() from the colorspace package is a pretty useful little tool (requires shiny and shinyjs to be installed, as it's a shiny app) that can be helpful for choosing colors. This guide may also come in handy. scale_fill_gradientn and use of the choose_color() to set your palette gives you complete control, but it has a tendency to make the transition kind of odd: this is why I like the first gradient shown above vs. the second.

Patterned fills

Currently, there are no good, simple methods to create patterned fill in ggplot to my knowledge. Could be useful to ask a question, but the best seems to be this answer from SO. Not really too satisfactory, IMO, but it seems at the moment to be the best that can be done.

chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Thanks so much for explaining how the gradient was calculated! If I want to use a gradient that is calculated on the rgb scale, it looks like I might have to make my own color palette. – ChEmIcaNdiOn May 19 '20 at 15:23