ggplot heatmap中的值不正常

时间:2016-11-28 02:39:19

标签: r ggplot2

这是我的原始数据框,名为e

structure(list(`Variance (2)` = c(48.4127, 5.28412698412698, 0.00520833333333333, 0.72083903433112), `Variance (4)` = c(42.0635, 11.7253968253968, 0.0164803125616411, 0.757277684759935), `Variance & Mean (2)` = c(73.8095, 8.63095238095238, 0.0292019422400468, 0.633561928614755), `Variance & Mean (4)` = c(28.5714, 33.4107142857143, 0.0258761022200942, 0.496305437129809), `Radar Only (2)` = c(73.8095, 18.3043650793651, 0.0923386443151634, 0.435493681015241), `Radar Only (4)` = c(51.5873, 76.5388888888889, 0.0563903328454791, 0.402952149580986), `All Data(2)` = c(73.8095, 36.3634920634921, 0.0717603398435939, 0.296927697111903), `All Data(3)` = c(69.8413, 52.168253968254, 0.065214418675562, 0.300126336907244), `All Data Scaled(2)` = c(64.2857, 52.1623015873016, 0.0657912811602958, 0.247276928682524), `All Data Scaled(4)` = c(67.4603, 84.1174603174603, 0.0380569462508109, 0.257546088468624)), .Names = 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)"), row.names = c(NA, 4L), class = "data.frame")
  • 我正在根据此数据框构建热图
  • 颜色应由缩放值确定
  • 标签应为原始值
  • 构建融化数据框时出了点问题

这是我到目前为止的代码:

f<-e
f<-t(f)    # transpose so we can scale
f<-apply(f,c(1,2),function(x) as.numeric(x)) # make numeric, cuz this factors them for some reason
f<-scale(f)
f<-t(f)    # transpose again to get in meltable format
rownames(f)<-c("Specificity","Connectivity","Dunn Index","Silhouette")

# A & C are better if they're higher, so I separate them out.
hi<-as.matrix(f[c("Specificity","Dunn Index"),])

oh<-melt(t(e[c(1,3),]))    # get the original values

hi.m <- melt(hi)
hi.m <- cbind(hi.m,oh[,3]) # cbind the original vals to scaled !!! ISSUE HERE !!!!
colnames(hi.m)[4]<-"ovals"

所以此时数据已经无序,我不知道为什么。

<小时/> 这是情节的代码:

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")

这是一个正在发生的事情的图像。红色箭头表示数据的位置: enter image description here

<小时/> 编辑:澄清。

关于最终产品应该如何看待存在一些混淆,所以我将包括这个以帮助:

数据应如下所示:

              "Specificity" "Dunn Index"
Variance (2)        48.4127 0.005208333 
Variance (4)        42.0635 0.016480313 
Variance & Mean (2) 73.8095 0.029201942 
Variance & Mean (4) 28.5714 0.025876102 
Radar Only (2)      73.8095 0.092338644 
Radar Only (4)      51.5873 0.056390333 
All Data(2)         73.8095 0.071760340 
All Data(3)         69.8413 0.065214419 
All Data Scaled(2)  64.2857 0.065791281 
All Data Scaled(4)  67.4603 0.038056946 

特异性介于27和74之间,Dunn指数介于0和1之间。

1 个答案:

答案 0 :(得分:1)

(我并不清楚错误是什么,而是选择关注图表而不是melt的结果,因为你在这个图上花了那么多精力。如果它&#39;真的是melt的结果是&#34;错误&#34;,那么你应该去掉所有关于ggplot2工作的东西。)

我可以重现图表,但我不认为有错误。我拿了你的数据集并在xtabs上对ovals - 数值标签值进行了 xtabs( ovals ~ Var2 +Var1, data=hi.m) Var1 Var2 A C Variance (2) 48.412700000 42.063500000 Variance (4) 73.809500000 28.571400000 Variance & Mean (2) 73.809500000 51.587300000 Variance & Mean (4) 73.809500000 69.841300000 Radar Only (2) 64.285700000 67.460300000 Radar Only (4) 0.005208333 0.016480313 All Data(2) 0.029201942 0.025876102 All Data(3) 0.092338644 0.056390333 All Data Scaled(2) 0.071760340 0.065214419 All Data Scaled(4) 0.065791281 0.038056946

> v2rlevs <- rev(levels(hi.m$Var2))
> hi.m$newV2 <- factor( as.character(hi.m$Var2), levels= v2rlevs)
> xtabs( ovals ~ newV2 +Var1, data=hi.m)
                     Var1
newV2                            A            C
  All Data Scaled(4)   0.065791281  0.038056946
  All Data Scaled(2)   0.071760340  0.065214419
  All Data(3)          0.092338644  0.056390333
  All Data(2)          0.029201942  0.025876102
  Radar Only (4)       0.005208333  0.016480313
  Radar Only (2)      64.285700000 67.460300000
  Variance & Mean (4) 73.809500000 69.841300000
  Variance & Mean (2) 73.809500000 51.587300000
  Variance (4)        73.809500000 28.571400000
  Variance (2)        48.412700000 42.063500000

这基本上就是你所看到的,只有行顺序被尊重。哟按照你的顺序获得价值,你可以反转这个因素的水平:

newV2

(这是ggplot2使用的默认排序的逆转,因为它从&#34; top&#34开始;然后关闭。如果你想要你的图表&#34;翻转&#34;在#34上;行轴&#34;正在请求认为,您只需要使用 public void Run() { window.Show(); window.Focus(); Initialize(); isRunning = true; canRender = true; timer = new Stopwatch(); timer.Start(); // the amount of milliseconds needed to pass before rendering next frame double frameCapCounter = 16.666666; while (isRunning) { Application.DoEvents(); if (window.Focused) { if (timer.ElapsedMilliseconds >= frameCapCounter) { canRender = true; frames += 1; // update amount of frames frameCapCounter += 16.666666; // increment counter } else { canRender = false; } // this is used to check if a second has passed, and if so // we set the fps variable to the amount of frames rendered // and reset all variables. if (timer.ElapsedMilliseconds >= 1000) { fps = frames; frames = 0; frameCapCounter = 0; timer.Restart(); } Update(); LateUpdate(); if (canRender) Render(); else { Thread.Sleep(1); } } } }

enter image description here