2015-05-04 92 views
4

我使用該R腳本:熱火地圖GGPLOT2

tableau <- read.table(
    text = 
    "Net B C D E.(e) F.(f) 
a 1.88 0.15 0.60 10.00 90.00 
b 2.05 0.23 0.51 55.00 80.00 
c 2.09 0.29 0.40 58.00 88.00 
d 2.07 0.52 0.36 80.00 84.00 
e 2.13 0.30 0.27 7.00 90.00", 
    header = TRUE) 

library(plyr) 
library(reshape) 
library(ggplot2) 
library(scales) 
tableau.m <- melt(tableau) 
tableau.m <- ddply(tableau.m, .(variable), transform, rescale = rescale(value)) 

(p <- ggplot(tableau.m, aes(variable, Net)) + 
    geom_tile(aes(fill = rescale), colour = "white") + 
    scale_fill_gradient(low = "white", high = "steelblue")) 

base_size <- 9 
p + theme_grey(base_size = base_size) + 
    labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
    scale_y_discrete(expand = c(0, 0)) + 
    theme(legend.position = "none", axis.ticks = element_blank(), 
     axis.text.x = element_text(size = base_size * 0.8, angle = 0, 
            hjust = 0, colour = "grey50")) 

tableau.s <- ddply(tableau.m, .(variable), transform, rescale = scale(value)) 

last_plot() %+% tableau.s 

我得到這個情節:

enter image description here

如果深藍色意味着更高的價值和白色意味着更低的值。

如何,如果可能的話,我可以改變這個代碼,以便:

  1. 從表中的值顯示在矩陣圖的相應單元?
  2. 熱圖的範圍不是在整個矩陣上計算,而是針對每列計算。因此,對於每個類別:B,C,D,E(e)和F(f),白色表示該列的較低值,而較深的藍色表示該列的較高值?

謝謝!

回答

8

要添加value爲文本標籤,每個單元格,則可以使用geom_text

p <- ggplot(tableau.m, aes(variable, Net)) + 
     geom_tile(aes(fill = rescale), colour = "white") + 
     scale_fill_gradient(low = "white", high = "steelblue") + 
     geom_text(aes(label=value)) 

# Add the theme formatting 
base_size <- 9 
p + theme_grey(base_size = base_size) + 
    labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
    scale_y_discrete(expand = c(0, 0)) + 
    theme(legend.position = "none", axis.ticks = element_blank(), 
     axis.text.x = element_text(size = base_size * 0.8, 
            angle = 0, hjust = 0, colour = "grey50")) 

關於第二個問題,當前的代碼已經採取的照顧。變量rescale分別縮放每列,因爲您已執行按variable分組的操作。由於rescalefill變量,所以爲了設置顏色值,每列值都從0重新調整爲1。您不需要代碼tableau.s ... last.plot...

下面是運行上述代碼後的情節。請注意,在每列中,最低值爲白色,最高值爲鋼藍色。 (您可能想改變「白色」,比方說,「gray90」邊框顏色,所以會有相鄰的白色方格之間的邊框):使用tidyrdplyr

enter image description here

+0

嗨@ eipi10,我意識到第一列的縮放方式應該與其他方法相反。也就是說,1.88應該是最黑的,而2.13應該是白的。是否有一種方法可以僅反轉一列的縮放比例? (我應該問這個新的問題嗎?) – Rodolphe

+0

我其實去了,並問了一個新的問題... http://stackoverflow.com/questions/30078074/rescaling-with-plyr-ddply-in-r 乾杯! – Rodolphe

2

類似的想法重塑數據長格式和ggvis繪製熱圖:

library(dplyr) 
library(ggvis) 
library(tidyr) 

tableau %>% 
    gather(variable, value, -Net) %>% 
    group_by(variable) %>% 
    mutate(scale = percent_rank(value)) %>% 
    mutate_each(funs(factor(.)), -value, -scale) %>% 
    ggvis(~variable, ~Net, fill=~scale) %>% 
    layer_rects(width = band(), height = band(), stroke := NA) %>% 
    layer_text(
    x = prop("x", ~variable, scale = "xcenter"), 
    y = prop("y", ~Net, scale = "ycenter",), 
    text:=~value, fontSize := 14, fontWeight := "bold", fill:="black", 
    baseline:="middle", align:="center") %>% 
    scale_nominal("x", padding = 0, points = FALSE) %>% 
    scale_nominal("y", padding = 0, points = FALSE) %>% 
    scale_nominal("x", name = "xcenter", padding = 1, points = TRUE) %>% 
    scale_nominal("y", name = "ycenter", padding = 1, points = TRUE) %>% 
    scale_numeric("fill", range = c("white", "steelblue")) %>% 
    add_axis("x", properties = axis_props(grid = list(stroke = NA))) %>% 
    add_axis("y", properties = axis_props(grid = list(stroke = NA))) %>% 
    hide_legend("fill") 

其中給出:

enter image description here