简体   繁体   English

R ggplot geom_tile - 如何获得更大范围的 colors?

[英]R ggplot geom_tile - How to get a larger range of colors?

I currently have a heatmap created from geom_tile which is filled by counts.我目前有一个从 geom_tile 创建的热图,它由计数填充。 However, the max count is 300000+ and the minimum is 1, and includes NA as well.但是,最大计数为 300000+,最小值为 1,并且还包括 NA。 I would like there to be a larger variation in colors, how can I achieve that?我希望 colors 有更大的变化,我该如何实现?

在此处输入图像描述

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.请记住,如果您的计数中有 40% 为 1,则无论转换如何,它们都将保持相同的颜色。 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.但是,如果您提到了一些高数字并且您没有转换数据,则 2、4 等的计数将显示为与您的 1 相同的颜色。 There are different techniques for left or right skewed distributions.对于左偏态或右偏态分布有不同的技术。 An interesting read can be found here: http://fmwww.bc.edu/repec/bocode/t/transint.html可以在这里找到有趣的阅读: http://fmwww.bc.edu/repec/bocode/t/transint.html

Common transformations include log10 (or another base), sqrt , 1 / n , etc. or a combination thereof.常见的转换包括log10 (或其他基数)、 sqrt1 / n等或它们的组合。

Here an example with n's that produce a more or less black spotted graph like yours on default, with a few transformations to create more "contrast"这是一个带有 n 的示例,默认情况下会产生或多或少的黑色斑点图,并进行一些转换以创建更多“对比度”

example例子

dt <- data.frame(
  x = sample(1:40, 100, replace = T),
  y = sample(1:40, 100, replace = T),
  n = c(rep(c(1:10 %o% 10^(1:7)), 5), sample(1:20, 50, replace = T))
)

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

I usually use the pheatmap function but sure with yours can also do it.我通常使用 pheatmap function 但你的肯定也可以。 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.在此示例中,我使用brewer.pal() function 来获取 colors 的调色板,但如果您知道颜色代码,您可以创建自己的调色板。 The output of this function is just a character vector with the color codes you chose.这个 function 的 output 只是一个字符向量,带有您选择的颜色代码。

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!例如,我经常使用的这个调色板是用 19 colors 代替,它提供了更大的多样性!

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

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

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