2012-12-22 13 views
8

Questa immagine heatmap è stata generata in Excel, ma come sto cercando di imparare RI vorrebbe sapere come fare una mappa termica del genere con R.2 colori in R con il colore centrale ancorato ad un valore specifico

Excel HeatMap

in questo momento, questo codice funziona, ma non come avrebbe dovuto ...

(p <- ggplot(melt(heat), aes(Var2, Var1)) 
+ geom_tile(aes(fill = value), colour = "white") 
+ scale_fill_gradient(low = "red", high = "green")) 

Vorrei avere 2 pendenze, verde (valore più alto) al bianco (il numero 1000) a rosso (valore più basso). È possibile in R?

dataset di calore:

> heat[0:10,0:10] 
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] 
[1,] 1000.000 0.000 0.0000 0.0000 757.0317 709.3896 843.7676 932.2801 0.0000 1016.7203 
[2,] 1087.658 1000.000 0.0000 0.0000 935.5829 854.5110 889.5042 1091.4610 929.1611 0.0000 
[3,] 1181.599 1361.953 1000.0000 0.0000 0.0000 1102.1590 1147.1300 984.1374 969.0718 1058.3456 
[4,] 1319.012 1405.954 1187.5215 1000.0000 0.0000 1093.8854 1195.7298 1077.0797 1119.4640 1159.5207 
[5,] 0.000 0.000 909.1927 817.5097 1000.0000 0.0000 1101.2891 1064.6516 1037.1623 990.3974 
[6,] 0.000 0.000 0.0000 0.0000 887.7498 1000.0000 1015.9835 1062.1668 1105.2163 983.2319 
[7,] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 1000.0000 911.0699 1026.1989 951.3572 
[8,] 0.000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 834.8725 927.6802 
[9,] 1261.824 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 795.6285 
[10,] 0.000 1121.210 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1000.0000 
+0

dovrebbe essere possibile se si dà un esempio reprooducible? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – agstudy

+0

esempio incluso. –

risposta

10

È possibile utilizzare? scale_fill_gradientn

si gving valori:

library(scales) 
    heat.dat <- heat[0:10,0:10] 
    dat <- expand.grid(var1=1:10, var2=1:10) 
    dat$value <- melt(heat.dat)$value 
    ggplot(dat, aes(x=var1,y=var2))+ 
     geom_tile(aes(fill = value),colour='white')+ 
     scale_fill_gradientn(colours=c("red","white","green"), 
     values = rescale(c(min(dat$value), 1000, max(dat$value))) 

enter image description here

Uso Lattice:

with(dat, 
levelplot(round(value,0)~y*x, 
      dat, 
      panel=function(...) { 
       arg <- list(...) 
       panel.levelplot(...) 
       panel.text(arg$x, arg$y,arg$z)}, 
      scales = list(y = list(at=y,labels=y), 
         x = list(at=y,labels=y)), 
      col.regions = colorRampPalette(c("red", "white", "green"),interpolate ='spline')) 
) 

enter image description here

+1

Grazie per l'aiuto. L'ho ottimizzato un po 'dopo un'altra ricerca su google: '(p <- ggplot (melt (heat [0: 10,0: 10]), aes (Var2, Var1)) + geom_tile (aes (fill = value), color = "white") + scale_fill_gradientn (colors = c ("rosso", "bianco", "verde"), valori = riscalata (c (min (calore [0: 10,0: 10]), 1000, max (heat [0: 10,0: 10]))), guide = "colorbar")) ' In questo modo il numero 1000 sarà sempre bianco e gli altri colori verranno posizionati di conseguenza. –

+0

risponde a questa domanda? ma penso che se quello che vuoi è mostrare i numeri come Excel è meglio, in questo caso usare il livelloplot del pacchetto reticolo, ecco un esempio http://stackoverflow.com/questions/13895480/how-to-show-matrix- values-on-levelplot/13895940 # 13895940 – agstudy

+0

Come si fa a fare in modo che le celle nei dati originali corrispondano alle celle che sono tracciate con ggplot. I valori sono invertiti. – GabrielMontenegro

0

Oltre alla risposta e commenti di/a agstudy, è può anche utilizzare la sintassi ggplot pura e mappare le etichette all'interno dei aes() tramite label:

heat=structure(c(1000, 1087.658, 1181.599, 1319.012, 0, 0, 0, 0, 261.824,0, 0, 1000, 1361.953, 1405.954, 0, 0, 0, 0, 0, 1121.21, 0, 0,1000, 187.5215, 909.1927, 0, 0, 0, 0, 0, 0, 0, 0, 1000, 817.5097,0, 0, 0, 0, 0, 57.0317, 935.5829, 0, 0, 1000, 887.7498, 0, 0,0, 0, 709.3896, 854.511, 1102.159, 093.8854, 0, 1000, 0, 0,0, 0, 843.7676, 889.5042, 1147.13, 1195.7298, 1101.2891, 015.9835,1000, 0, 0, 0, 932.2801, 1091.461, 984.1374, 1077.0797, 1064.6516,1062.1668, 911.0699, 1000, 0, 0, 0, 929.1611, 969.0718, 1119.464,1037.1623, 1105.2163, 1026.1989, 834.8725, 1000, 0, 1016.7203,0, 058.3456, 1159.5207, 990.3974, 983.2319, 951.3572, 927.6802,795.6285, 1000), Dim c(10L, 10L), .Dimnames = list(NULL, NULL)) 

    heat_melted = reshape2::melt(heat[10:0,0:10]) 

    ggplot(heat_melted, aes(Var2, Var1, label = round(value))) + 
    geom_tile(aes(fill = value), colour = "white") + 
    scale_fill_gradientn(colours=c("red", "white", "green"), 
    values=rescale(c(min(heat_melted$value), 1000, max(heat_melted$value))), guide="colorbar") + 
    geom_text() 

plot

Problemi correlati