如何在格子布局中制作热图样式的双变量直方图?

时间:2013-02-05 01:47:59

标签: r lattice

采用以下示例数据:

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

我们可以按如下方式生成散点图矩阵:

splom(df)

enter image description here

但由于重叠点数量众多,很难衡量密度。

是否有一种直接的方法可以用双变量直方图热图替换每个绘图,就像squash生成的那样?

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

enter image description here

3 个答案:

答案 0 :(得分:8)

panel.hexbinplot方便大型数据集。

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

enter image description here

您可以像这样自定义面板功能:

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)

您可以使用style参数。

enter image description here

答案 1 :(得分:4)

这是另一个与原始请求更符合的选项

# 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 :(得分:0)

这不是您要求的方法,但可以帮助您解决您所描述的基本问题:)

# 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)
相关问题