2012-03-08 37 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

相关问题