2016-11-27 80 views
1

這裏是我試圖繪製數據:GGplot熱圖對每瓦2個標籤

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. 在第一aes()
  3. 配售
  4. 在第一aes()
  5. 刪除geo_tile配售fill(包括標籤仍然顯示)

回答

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

對。當我最初拆分我的數據框時,我忘記刪除2個變量,因此它們都堆疊在一起。奇怪的是,ggplot沒有給出錯誤。我覺得其他一切都是錯誤的。只是學習ggplot的方式。這是神祕的。謝謝您的幫助。 –