[英]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.