2013-02-05 6 views
6

Prendere i seguenti dati esempio:Come si crea un istogramma bivariato in stile heatmap in un layout reticolare?

x <- rnorm(10000) 
y <- rnorm(10000) * x 
z <- rnorm(10000) * y 
df <- data.frame(x,y,z) 

possiamo produrre una matrice diagramma a dispersione come segue:

splom(df) 

enter image description here

Ma a causa del gran numero di punti di sovrapposizione è difficile misurare la densità.

Esiste un modo semplice per sostituire ciascun grafico con una mappa di calore dell'istogramma bivariata, come quelli prodotti da squash?

library(squash) 
hist2(df$x, df$y) 

enter image description here

risposta

8

Il panel.hexbinplot è conveniente per grandi insiemi di dati.

library(hexbin) 
splom(df, panel=panel.hexbinplot) 

enter image description here

È possibile personalizzare la funzione del pannello come questo:

library(hexbin) 
splom(df, 
     panel = function(x, y, ...){ 
     panel.hexbinplot(x, y, style = "nested.lattice", 
         type = c("g", "smooth"),col='blue', ...) 
     }, 
     pscale=0, varname.cex=0.7) 

Si può giocare con teh parametro style.

enter image description here

+0

Questo sembra molto promettente, ma ho il seguente errore: Errore in grid.Call.graphics (L_downviewport, nome $ name, strict): Viewport 'plot_01.panel.1.1.off.vp' non è stato trovato – saffsd

+0

@saffsd è strano. Provalo in una nuova sessione R per favore. – agstudy

+0

L'errore persiste in una nuova sessione. Per riferimento: R versione 2.15.1 (2012-06-22) - "Roasted Marshmallows" Piattaforma: x86_64-pc-linux-gnu (64-bit) – saffsd

0

questo non è il metodo che hai chiesto, ma aiuta a risolvere il problema fondamentale che hai descritto :)

# run the code you've provided 
library(lattice) 
x <- rnorm(10000) 
y <- rnorm(10000) * x 
z <- rnorm(10000) * y 
df <- data.frame(x,y,z) 

# figure out what ten percent of the total records are 
ten.percent <- nrow(df)/10 

# create a new data frame `df2` containing 
# a randomly-sampled ten percent of the original data frame 
df2 <- df[ sample(nrow(df) , ten.percent ) , ] 

# now `splom` that.. and notice it's easier to see densities 
splom(df2) 
4

ecco un'altra opzione che è più in- linea con la richiesta originale

# run the code you've provided 
library(lattice) 
x <- rnorm(10000) 
y <- rnorm(10000) * x 
z <- rnorm(10000) * y 
df <- data.frame(x,y,z) 

# look at each of these options one-by-one.. go slowly! 

# here's your original 
splom(df) 


# here each point has been set to very transparent 
splom(df , col="#00000005") 

enter image description here

# here each point has been set to moderately transparent 
splom(df , col="#00000025") 

enter image description here

# here each point has been set to less transparent 
splom(df , col="#00000050") 

enter image description here

+2

È consigliabile ridurre anche i punti, ad es. 'splom (df, col =" # 00000040 ", pch = '.')'. –

Problemi correlati