带有连续彩虹色的热图 [英] Heatmap with continuous rainbow colours
问题描述
首先,我不得不说,我在stackoverflow和其他地方阅读了许多有关heatmap和ggplot2的主题.但是我的问题还没有解决.
First of all I have to say that I read many threads about heatmap and ggplot2 here in stackoverflow and elsewhere. But my problem isn't solved yet.
我有以下数据集:
Var1 Var2 value
1 -197.5 -197.5 0
2 -192.5 -197.5 0
3 -187.5 -197.5 0
4 -182.5 -197.5 0
5 -177.5 -197.5 0
6 -172.5 -197.5 0
.....
该值应该是颜色,并且右侧的图例会很好.
The value should be the colour, and a legend on the right side would be nice.
library(ggplot2)
ggheatmap <- ggplot(data = dat.plot, aes(x=Var1, y=Var2, fill=value)) +
geom_raster()+
scale_fill_gradientn(colours=rainbow(100))+
theme(axis.text.x = element_text(angle = 0))+
coord_fixed()
print(ggheatmap)
结果是:
我希望有一个正常"的彩虹标度,从红色=高到橙色,黄色,绿色,浅蓝色,深蓝色=低,而没有给出固定的离散颜色,例如使用scale_fill_gradient2. 我不知道为什么彩虹"以红色=高端以其他红色结尾...
I would like to have a "normal" rainbow scale from red=high over orange, yellow, green, light blue, dark blue=low without giving fixed discrete colours as one can do it, e.g. with scale_fill_gradient2. I wonder why "rainbow" starts with red=high end ends with some other red...
另一个问题:我如何添加一些东西来平滑"热图,以使人到处都看不到边缘"?
The other question: How can I add something to "smooth" the heatmap so that one doesn't see the "edges" everywhere?
推荐答案
简短答案:函数rainbow()
发疯了当您通过100
时,您要求使用100
不同的颜色
Short answer: function rainbow()
goes nuts when you pass 100
as you're asking for 100
different colors.
您应该做什么:将n
传递给rainbow()
以获取所需的颜色数.如果要从蓝色变为红色,则还必须使用功能rev()
将其包装.
What should you do: pass n
to rainbow()
for how many colors you want. If you want to go from blue to red then you also have to wrap it with function rev()
.
library(egg)
library(ggplot2)
library(reshape2)
# Heatmap number of rows/columns
Nvalue <- 1e2
# n for colors passed to function rainbow
nColor <- c(1:10, seq(20, 100, 20))
# dummy data
df <- melt(matrix(rnorm(N^2), N))
plotList <- list()
for(i in seq_along(nColor)) {
plotList[[i]] <- ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(rainbow(nColor[i]))) +
labs(title = paste0("rainbow(", nColor[i], ")"),
x = NULL,
y = NULL,
fill = NULL) +
theme_void()
}
ggarrange(plots = plotList)
在他指定了OP指定的颜色之后,则可以通过十六进制矢量:
After OP specified colors he wants then passing hex vector should work:
hex <- c("#FF0000", "#FFA500", "#FFFF00", "#008000", "#9999ff", "#000066")
ggplot(df, aes(Var1, Var2, fill = value)) +
geom_raster() +
scale_fill_gradientn(colours = rev(hex)) +
labs(x = NULL,
y = NULL,
fill = NULL) +
theme_void()
这篇关于带有连续彩虹色的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!