geom_tile热图以不同的高填充颜色为基础 [英] geom_tile heatmap with different high fill colours based on factor
问题描述
我有兴趣在 ggplot2 中用 geom_tile
构建一个使用基于因子的不同渐变高颜色的热图。
I'm interested in building a heatmap with geom_tile
in ggplot2 that uses a different gradient high color based on a factor.
下面的图表根据 xy_type
创建了单个图块为蓝色或红色的情节,但没有渐变。
The plot below creates the plot where the individual tiles are colored blue or red based on the xy_type
, but there is no gradient.
ggplot() +
geom_tile(data=mydata, aes(x=factor(myx), y=myy, fill=factor(xy_type))) +
scale_fill_manual(values=c("blue", "red"))
下面的图不使用 xy_type
因子来选择颜色,但是我得到了一个基于 xy_avg_value
。
The plot below does not use the xy_type
factor to choose the color, but I get a single group gradient based on the xy_avg_value
.
ggplot() +
geom_tile(data=mydata, aes(x=factor(myx), y=myy, fill=xy_avg_value))
融合这两块地块?我可以使用 facet_grid(xy_type〜。)
来创建此数据的单独绘图,并使用渐变。因为这最终将成为一个地图(x〜y坐标),所以我想找到一种方法来在单个 geom_tile
地图中显示不同的渐变。
Is there a technique to blend these two plots? I can use a facet_grid(xy_type ~ .)
to create separate plots of this data, with the gradient. As this is ultimately going to be a map (x~y coordinates), I'd like to find a way to display the different gradient together in a single geom_tile
map.
推荐答案
一般而言, ggplot2 不允许多种比例的单一类型(即多种颜色或填充所以我怀疑这不是(很容易)。
In general, ggplot2 does not permit multiple scales of a single type (i.e. multiple colour or fill scales), so I suspect that this isn't (easily) possible.
我能想到的最接近的近似值是:
The best nearest approximation I can come up with is this:
df <- data.frame(expand.grid(1:5,1:5))
df$z <- runif(nrow(df))
df$grp <- rep(letters[1:2],length.out = nrow(df))
ggplot(df,aes(x = Var1,y = Var2,fill = factor(grp),alpha = z)) +
geom_tile() +
scale_fill_manual(values = c('red','blue'))
但是要得到一个合理的传说很难。
But it's going to be tough to get a sensible legend.
这篇关于geom_tile热图以不同的高填充颜色为基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!