R ggplot2热图,强制使用自定义范围的离散比例,添加网格到地图

时间:2015-07-03 13:36:04

标签: r ggplot2

我创建了下面的热图

heatmap

我使用了这段代码:

`
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等范围是可以接受的,但我更喜欢规模上的单个自然数(而不是值范围)
  • 在热图上添加一个细网格,因此所有瓷砖(包括白色瓷砖)都被它隔开
  • 将NA值的灰色更改为浅灰色

帮助赞赏,以及更短的代码 谢谢!

1 个答案:

答案 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=)

enter image description here