Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a heatmap-style bivariate histogram in a lattice layout?

Tags:

r

lattice

Take the following example data:

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

We can produce a scatter plot matrix as follows:

splom(df)

enter image description here

But due to the large number of overlapping points it is hard to gauge the density.

Is there a straightforwards way to replace each plot with a bivariate histogram heatmap, like those produced by squash?

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

enter image description here

like image 853
saffsd Avatar asked Feb 05 '13 01:02

saffsd


2 Answers

The panel.hexbinplot is convenient for large datasets.

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

enter image description here

You can customize the panel function like this :

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)

You can play with teh style parameter.

enter image description here

like image 93
agstudy Avatar answered Sep 23 '22 08:09

agstudy


here's another option that's more in-line with your original request

# 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

like image 44
Anthony Damico Avatar answered Sep 22 '22 08:09

Anthony Damico