2013-02-05 21 views
6

看看下面的例子數據:如何在格子布局中製作熱圖式雙變量直方圖?

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

我們可以生產散點圖矩陣如下:

splom(df) 

enter image description here

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

是否有更換用雙變量直方圖熱圖的每個情節,像那些由squash產生straightforwards方式?

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

enter image description here

回答

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

+0

這看起來很有希望,但我得到以下錯誤: grid.Call.graphics(L_downviewport,name $ name,strict)中的錯誤: 未找到視口'plot_01.panel.1.1.off.vp' – saffsd

+0

@saffsd它很奇怪。請嘗試在新的R會話。 – agstudy

+0

該錯誤在新會話中持續存在。爲了參考,R版本2.15.1(2012-06-22) - 「烤棉花糖」 平臺:x86_64的-PC-Linux的GNU(64位) – saffsd

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) 
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 ='。')'。 –