R ggplot2热图,使用自定义范围强制离散比例,将网格添加到地图 [英] R ggplot2 heatmap, force discrete scale with custom range, add grid to map
问题描述
我在热图下面创建了
我使用了以下代码:
library(ggplot2)
library(reshape2)
winners_min_expd0 <- matrix(c(NA, 2, 2, 3, NA, 3, 2, 2, NA), nrow=3, ncol=3, byrow = T)
colnames(winners_min_expd0) <- c("homophily","heterophily","preferential")
rownames(winners_min_expd0) <- c("homophily","heterophily","preferential")
melted_min_expd0 <- melt(winners_min_expd0)
colnames(melted_min_expd0) <- c("winning", "looses", "times")
p_min_expd0 <- ggplot(melted_min_expd0, aes(looses, winning)) +
geom_tile(aes(fill = times), colour = "white") +
scale_fill_gradient(low = "white", high = "red") +
ggtitle("Min function, exp decay off") +
scale_y_discrete(limits=c("preferential", "heterophily", "homophily")) +
xlab("(x) looses against (y)") + ylab("(y) winning over (x)")
p_min_expd0
我想:
- 将时间"栏重新调整为范围
<0:5>
,我知道没有{0,1,4,5}
值,但我希望有这个范围 - 使"times"热标度离散,这意味着一种纯色表示一个数字,例如
5 = hot red
,4 = red
等(可能重复:>带有颜色的ggplot2热图对于范围值),可以接受像<0,1)
这样的范围,但我更喜欢刻度上的单个自然数(而不是值范围) - 在热图上添加一个细网格,以便所有图块(包括白色图块)都被其分隔
- 将NA值的灰色更改为更浅的灰色
- rescale "times" bar to range
<0:5>
, I understand there are no{0,1,4,5}
values, but I'd like to have this range - make the "times" heat scale discrete, which means one solid color for one number, e.g.
5 = hot red
,4 = red
, etc. (possible duplicate: ggplot2 heatmap with colors for ranged values), ranges like<0,1)
are acceptable, but I prefer single natural numbers on the scale (instead of value ranges) - add a thin grid on the heatmap, so all tiles (including white ones) are separated by it
- change the gray color of NA values to some lighter gray
帮助表示赞赏,以及较短的代码.
谢谢!
Help appreciated, as well as shorter code.
Thanks!
推荐答案
以下是一些修改
p_min_expd0 <- ggplot(melted_min_expd0, aes(looses, winning)) +
geom_tile(aes(fill = cut(times, breaks=0:5, labels=1:5)), colour = "grey") +
ggtitle("Min function, exp decay off") +
scale_y_discrete(limits=c("preferential", "heterophily", "homophily")) +
scale_fill_manual(drop=FALSE, values=colorRampPalette(c("white","red"))(5), na.value="#EEEEEE", name="Times") +
xlab("(x) looses against (y)") + ylab("(y) winning over (x)")
由于您需要离散的比例尺,因此您可以自己使用cut()
进行转换,从而照顾您的前两个愿望".这使连续变量成为离散变量.由于我们现在使用的是离散刻度,因此必须使用scale_fill_manual
自行设置颜色渐变. 网格"颜色是通过geom_tile
中的colour=
参数设置的. NA颜色用scale_fill_maual(na.value=)
设置.
Since you want a discrete scale, you can take care of your first two "wishes" by doing the conversion yourself with cut()
. This makes a discrete variable from a continuous one. Since we are using a deiscrete scale now, we must set the color ramp ourselves with the scale_fill_manual
. The "grid" color is set with the colour=
parameter in geom_tile
. The NA color is set with scale_fill_maual(na.value=)
.
这篇关于R ggplot2热图,使用自定义范围强制离散比例,将网格添加到地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!