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

7

请看以下示例数据:

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个回答

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


这看起来非常有前途,但我遇到了以下错误: Error in grid.Call.graphics(L_downviewport, name$name, strict) : Viewport 'plot_01.panel.1.1.off.vp' 未找到 - saffsd
@saffsd 这很奇怪。请尝试在新的 R 会话中运行它。 - agstudy
错误在新的会话中仍然存在。参考信息:R版本2.15.1(2012年6月22日)--“烤棉花糖”,平台:x86_64-pc-linux-gnu(64位)。 - saffsd
@saffsd 我现在无法在 Linux 版本上测试它。但在 Windows 上它可以工作。 - agstudy
问题出在 type=XXX 参数上,去掉它可以生成一个可用的图表。 - saffsd
类型“g”表示添加烤架,平滑则表示获取蓝线...但所有这些都是可选的,与您的问题无关。 - agstudy

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
将点的大小缩小也是一个好主意,例如splom(df, col="#00000040", pch='.') - Andy W

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接