簡體   English   中英

如何在格子布局中制作熱圖樣式的雙變量直方圖?

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

以下示例數據為例:

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

我們可以生成散點圖矩陣,如下所示:

splom(df)

在此輸入圖像描述

但由於大量的重疊點,很難測量密度。

是否有一種直接的方法用雙變量直方圖熱圖替換每個圖,就像壁球產生的那樣?

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

在此輸入圖像描述

panel.hexbinplot方便大型數據集。

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

在此輸入圖像描述

您可以像這樣自定義面板功能:

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)

你可以玩teh style參數。

在此輸入圖像描述

這是另一個更符合您原始請求的選項

# 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" )

在此輸入圖像描述

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

在此輸入圖像描述

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

在此輸入圖像描述

這不是您要求的方法,但可以幫助您解決您所描述的基本問題:)

# 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM