简体   繁体   English

使用 ggplot2 绘制大范围热图

[英]Plotting heatmap with large range with ggplot2

Suppose I have a data with a very big range假设我有一个范围很大的数据

data = c(1:5, 30:40, 58, 200:210, 400)

I tried to plot it with the R packages 'RColorBrewer' but the values 1:5 are nearly white color.我尝试使用 R 包“RColorBrewer”来 plot 它,但值 1:5 几乎是白色。

How can I plot a nice heatmap?我怎样才能 plot 一个漂亮的热图? Like from 1:5 is changing gradually from white to green, then 30:40 is changing from blue to purple... etc But another problem is the single value like 58....就像从 1:5 逐渐从白色变为绿色,然后 30:40 从蓝色变为紫色......等等但另一个问题是像 58 这样的单一值......

As pointed out in the comments, it seems unlikely that your desired palette can be fully automated.正如评论中所指出的,您想要的调色板似乎不太可能完全自动化。 However, it is not too hard to manually create a bespoke palette.但是,手动创建定制调色板并不难。

In the following, I've taken some base R colour names and interpolated these using the built in function colorRampPalette , wrapped in a helper f just for convenience.在下文中,我采用了一些基本的 R 颜色名称,并使用内置的 function colorRampPalette对这些颜色名称进行了插值,为了方便起见,将其包裹在帮助程序f中。

In this way, you can create whatever palette you like:通过这种方式,您可以创建任何您喜欢的调色板:

library(ggplot2)

data <- c(1:5, 30:40, 58, 200:210, 400)

f <- function(n, col1, col2 = NULL) colorRampPalette(c(col1, col2))(n)

colours <- c(
  f(5, "white", "cyan"),
  f(11, "blue", "purple"),
  f(1, "violet"),
  f(11, "pink", "red"),
  f(1, "black")
)

ggplot(NULL, aes(x = factor(data), y = 1, fill = factor(data))) +
  geom_tile() +
  scale_fill_manual(values = colours) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) # Just to fit reprex

Created on 2020-05-22 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 5 月 22 日创建

Obviously, you'll have to play around with the actual colour values to get something that looks good for your data.显然,您必须使用实际的颜色值来获得适合您的数据的东西。 Note that you don't need to use the colour names defined by R.请注意,您不需要使用 R 定义的颜色名称。 colorRampPalette (and hence f ) also takes hex colours, for instance, which you can grab from ColorBrewer , if you particularly like some of those.例如, colorRampPalette (因此f )也采用十六进制颜色,如果您特别喜欢其中的一些,您可以从ColorBrewer中获取。

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

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