2013-04-16 66 views
3

這裏的數據和hypithesis:與熱圖條XY散點圖在邊緣中的R

set.seed(1234) 
myd <- data.frame (X = rnorm (100), Y = rnorm (100, 10, 3)) 

只是catorizing X和Y,有時這可能是除了X和Y 不同變量並且是類本身

myd$xcat <- cut (myd$X, 10) 
myd$ycat <- cut (myd$Y, 10) 

我要讓好的情節類似以下,其中catories正在策劃爲熱圖地塊條 enter image description here

require(ggplot2) 
ggplot(myd, aes(x=X, y=Y)) + geom_point(shape=1) + theme_bw() 

這是可能的ggplot2或其他軟件包或需要專門的解決方案嗎?

回答

5

實現此目的的一種方法是用ggplot2製作三個獨立的地塊,然後使用viewport()grid.layout()將它們排列在一起。

第一個圖只包含中間部分(散點圖)。對於x和y軸,pxpy是熱圖(用geom_tile()製造)。最重要的部分是在圖中使用相同的theme()設置(只需將x更改爲y)。對於某些元素使用color="white"以確保該元素有一個位置(具有正確的尺寸),但它們在圖上不可見。

#Scatter plot without axis titles 
p<-ggplot(myd, aes(x=X, y=Y)) + geom_point(shape=1) + 
    theme_bw() + theme(axis.title=element_blank()) 

#tile plot for the x axis 
px<-ggplot(myd,aes(x=xcat,y=1,fill=xcat))+geom_tile()+ 
    scale_x_discrete(expand=c(0,0))+ 
    scale_fill_hue(h=c(0,180))+ 
    scale_y_continuous(expand=c(0,0),breaks=1,labels="10")+ 
    theme(legend.position="none", 
     axis.title=element_blank(), 
     axis.text.x=element_blank(), 
     axis.ticks.x=element_blank(), 
     axis.text.y=element_text(color="white"), 
     axis.ticks.y=element_line(color="white")) 

#tile plot for the y axis 
py<-ggplot(myd,aes(x=1,y=ycat,fill=ycat))+geom_tile()+ 
    scale_y_discrete(expand=c(0,0))+ 
    scale_x_continuous(expand=c(0,0),breaks=1,labels="1")+ 
    scale_fill_hue(h=c(181,360))+ 
    theme(legend.position="none", 
     axis.title=element_blank(), 
     axis.text.y=element_blank(), 
     axis.ticks.y=element_blank(), 
     axis.text.x=element_text(color="white"), 
     axis.ticks.x=element_line(color="white")) 

#Define layout for the plots (2 rows, 2 columns) 
layt<-grid.layout(nrow=2,ncol=2,heights=c(7/8,1/8),widths=c(1/8,7/8),default.units=c('null','null')) 
#View the layout of plots 
grid.show.layout(layt) 

#Draw plots one by one in their positions 
grid.newpage() 
pushViewport(viewport(layout=layt)) 
print(py,vp=viewport(layout.pos.row=1,layout.pos.col=1)) 
print(p,vp=viewport(layout.pos.row=1,layout.pos.col=2)) 
print(px,vp=viewport(layout.pos.row=2,layout.pos.col=2)) 

enter image description here

+0

非常好的謝謝 – jon

3

base情節:A液

brX <- seq(min(myd$X),max(myd$X),length=11) 
brY <- seq(min(myd$Y),max(myd$Y),length=11) 

layout(matrix(c(1,0,2,3),nrow=2),width=c(2,8),height=c(8,2)) 
par(mar=c(0,3,5,0)) 
plot(NA,ylim=range(myd$Y),xlim=c(0,1),axes=F,ann=F,xaxs="i") 
rect(0,brY[-length(brY)],1,brY[-1], 
    col=colorRampPalette(c("red","yellow","green"))(length(brY)-1)) 

par(mar=c(0,0,5,5)) 
plot(NA,xlim=range(myd$X),ylim=range(myd$Y),ann=F,xaxt="n",yaxt="n") 
abline(h=pretty(myd$Y),v=pretty(myd$X), col="grey95") 
points(myd$X,myd$Y,pch=21) 
axis(3) 
axis(4) 

par(mar=c(3,0,0,5)) 
plot(NA,xlim=range(myd$X),ylim=c(0,1),axes=F,ann=F,yaxs="i") 
rect(brX[-length(brX)],0,brX[-1],1, 
    col=colorRampPalette(c("blue","white","red"))(length(brX)-1)) 

enter image description here