2012-03-08 87 views
4

我想要做一個按x軸上的兩個離散變量排序的小平面ggplot。問題是我想讓垂直相鄰的條目都觸摸。目前,根據哪個水平的因子位於頂部圖與底部之間存在行之間的空間。對不起,這個可重複的例子有點冗長。ggplot geom_tile與小平面的間距

npats=20 

simsympt=c(id=1,date=1,tx="control",sympt=0) 

for(i in 1:npats) 

{ days=abs(round(rnorm(1,100,40),0)) 
    id=rep(as.character(i),days) 
    date=1:days 
    tx=rep(sample(c("control","treatment"),1),days) 
    sympt= sample(0:10, days,p=c(12,3,3,2,1,1,1,1,1,1,1),rep=T) 

    simsympt=  rbind(simsympt,  cbind(id,date,tx,sympt)) 
    } 
     ####tidy things up a bit 
    simsympt=data.frame(simsympt)[-1,] 
    colnames(simsympt)=c('id','date','tx','level') 
    simsympt$date=as.numeric(as.character(simsympt$date)) 
    simsympt$level=as.numeric(as.character(simsympt$level)) 
    #simsympt$id=as.numeric(as.character(simsympt$id)) 

    head(simsympt) 

##now the important stuff 

p <- ggplot(simsympt, aes(x=date,y=id))  
p= p + geom_tile(aes(fill=level)) + 
     facet_grid(tx~.,drop=T,space="free")+ 
     scale_y_discrete(expand=c(0,0),drop=T) 
p 

No description here

所有我需要的是除去在頂部和底部圖(小面)都行之間的所有的垂直空間。例如,由於ID號15在對照組中,治療組中不應該有她的行。 謝謝, 賽斯

回答

11
library("grid") 
p + opts(panel.margin=unit(0,"pt")) 

編輯:後進一步澄清

拆錯了空間。您想要的是將您的facet_grid呼叫更改爲包含scales="free"參數。

p <- ggplot(simsympt, aes(x=date,y=id))  
p= p + geom_tile(aes(fill=level)) + 
    facet_grid(tx~.,drop=T,space="free",scales="free")+ 
    scale_y_discrete(expand=c(0,0),drop=T) 

enter image description here

+0

這工作完美。謝謝 – Seth 2012-03-08 23:28:13

相關問題