Home > Software design >  R ggplot geom_tile - How to get a larger range of colors?
R ggplot geom_tile - How to get a larger range of colors?

Time:01-21

I currently have a heatmap created from geom_tile which is filled by counts. However, the max count is 300000 and the minimum is 1, and includes NA as well. I would like there to be a larger variation in colors, how can I achieve that?

enter image description here

CodePudding user response:

I usually use the pheatmap function but sure with yours can also do it. In this example I use the brewer.pal() function to get a palette of colors but you can create your own palette if you know the color codes. The output of this function is just a character vector with the color codes you chose.

require(pheatmap)
require(RColorBrewer)
P <- brewer.pal(9,"Blues")
pheatmap(as.matrix(dataframe), scale = "none", color=brewer.pal(9,"Blues"), 
        treeheight_row = 0,treeheight_col = 0,
        main = "title")

for example this palette I use a lot is with 19 colors instead, which gives larger variety!

palette <- c('#E0FFFF',   '#B0E0E6',  '#FFFACD',  '#FFFF00',
  '#FFD700', '#FFA500', '#FF8C00', '#FF7F50', '#DAA520', 
  '#B8860B', '#B8860B',  '#FA8072','#FF6347',  '#FF4500',
  '#DC143C', '#B22222',  '#A52A2A', '#8B0000', '#800000')

CodePudding user response:

When you have a skewed distribution you need to transform your data to get enough contrast. Keep in mind though that if you have like 40% of your counts being 1, no matter the transformation, they will remain the same color. However if you have some high numbers as you mention and you do not transform your data, counts of 2, 4, etc. will show up in the same color as your 1's. There are different techniques for left or right skewed distributions. An interesting read can be found here: enter image description here

ggplot(dt, aes(x, y, fill = sqrt(n)))   geom_tile()

enter image description here

ggplot(dt, aes(x, y, fill = log10(n)))   geom_tile()

enter image description here

ggplot(dt, aes(x, y, fill = log(n, 100)))   geom_tile()

enter image description here

ggplot(dt, aes(x, y, fill = sqrt(log(n, 100))))   geom_tile()

enter image description here

  •  Tags:  
  • Related