我创建了下面的热图
我使用了这段代码:
`
User u = new User();
我想:
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}
,5 = hot red
等(可能重复:ggplot2 heatmap with colors for ranged values),4 = red
等范围是可以接受的,但我更喜欢规模上的单个自然数(而不是值范围) 帮助赞赏,以及更短的代码 谢谢!
答案 0 :(得分:3)
以下是一些修改
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()
自行完成转换,从而照顾您的前两个“愿望”。这使得离散变量来自连续变量。由于我们现在使用的是deiscrete秤,我们必须使用scale_fill_manual
设置颜色渐变。使用colour=
中的geom_tile
参数设置“网格”颜色。 NA颜色设置为scale_fill_maual(na.value=)
。