GGplot热图在每个图块上有2个标签

时间:2016-11-27 20:55:33

标签: r ggplot2 heatmap

这是我试图绘制的数据:

structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("Specificity", "Dunn Index")), Var2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), class = "factor", .Label = c("Variance (2)", "Variance (4)", "Variance & Mean (2)", "Variance & Mean (4)", "Radar Only (2)", "Radar Only (4)", "All Data(2)", "All Data(3)", "All Data Scaled(2)", "All Data Scaled(4)")), value = c(-0.692279597863213, -1.48943741434953, -1.09355531182318, -1.08413861964885, 0.912823257976641, -0.626715742011029, -1.94602923014127, -0.746300681279627, 0.912823257976641, 1.64344717562082, -0.491325775754128, 0.350878495792349, 0.912823257976641, 0.90352725291858, 0.661946945469388, 0.668160073306832, 0.311225652165799, 0.688901920415944, 0.511547544016677, -0.308322460765497, -0.692279597863213, -1.48943741434953, -1.09355531182318, -1.08413861964885, 0.912823257976641, -0.626715742011029, -1.94602923014127, -0.746300681279627, 0.912823257976641, 1.64344717562082, -0.491325775754128, 0.350878495792349, 0.912823257976641, 0.90352725291858, 0.661946945469388, 0.668160073306832, 0.311225652165799, 0.688901920415944, 0.511547544016677, -0.308322460765497), ovals = c(48.41, 42.06, 73.81, 28.57, 73.81, 51.59, 73.81, 69.84, 64.29, 67.46, 5.28412698412698, 11.7253968253968, 8.63095238095238, 33.4107142857143, 18.3043650793651, 76.5388888888889, 36.3634920634921, 52.168253968254, 52.1623015873016, 84.1174603174603, 0.00520833333333333, 0.0164803125616411, 0.0292019422400468, 0.0258761022200942, 0.0923386443151634, 0.0563903328454791, 0.0717603398435939, 0.065214418675562, 0.0657912811602958, 0.0380569462508109, 0.72083903433112, 0.757277684759935, 0.633561928614755, 0.496305437129809, 0.435493681015241, 0.402952149580986, 0.296927697111903, 0.300126336907244, 0.247276928682524, 0.257546088468624)), .Names = c("Var1", "Var2", "value", "ovals"), row.names = c(NA, -40L), class = "data.frame")

这是我的代码:

ggplot(hi.m, aes(Var1, Var2)) +
  geom_tile(aes(fill = value)) + 
  geom_label(aes(label = round(ovals,4)),fill="white",alpha=0.65) +
  scale_fill_viridis(name="Scaled\nValue",option="plasma")

我有一个融合的数据框hi.m,其中包含原始值的缩放版本,但我希望原始值为标签,因此我将它们添加到hi.m作为新列,椭圆形。

这是绘图结果,看起来缩放值位于未缩放值的顶部,但它们都被舍入到4位小数:

enter image description here

这是我尝试过的:

  1. 使用par(op)
  2. 重置图形参数
  3. label放入第一个aes()
  4. fill放入第一个aes()
  5. 删除geo_tile(两个标签仍然显示)

1 个答案:

答案 0 :(得分:1)

您的数据对Var1Var2都有多个观察结果,因此它会覆盖标签上的每个值。如果可以,请在绘图之前汇总数据框。  enter image description here下面是使用dplyr的示例,我使用了mean,但这可能是您选择的另一个聚合函数(您必须自己规范化并且您可能想要做在您的工作流程的上游):

library(dplyr)
library(ggplot2)
library(viridis)

plot_data <- hi.m %>% 
  group_by(Var1, Var2) %>% 
  summarize_each(funs(mean), value, ovals)

ggplot(plot_data, aes(Var1, Var2)) +
  geom_tile(aes(fill = value)) + 
  geom_label(aes(label = round(ovals,4)),fill="white",alpha=0.65) +
  scale_fill_viridis(name="Scaled\nValue",option="plasma")