ggplot带有构面的geom_tile间距 [英] ggplot geom_tile spacing with facets

查看:223
本文介绍了ggplot带有构面的geom_tile间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过x轴上的两个离散变量排序分面ggplot。问题是我想让垂直相邻的条目都触摸。目前,根据哪个水平的因子位于顶部图与底部之间存在行之间的空间。

  npats = 20 
$ b $ (1:npats)

{天数= 1,日期= 1,tx =控制,症状= 0)

abs(round(rnorm(1,100,40),0))
id = rep(as.character(i),days)
date = 1:days
tx = rep c(对照,治疗),1),天)
症状=样本(0:10,天,p = c(12,3,3,2,1,1,1,1, 1,1,1),rep = T)

simsympt = rbind(simsympt,cbind(id,date,tx,sympt))
}
#### tidy事情有点
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 .nu​​meric(as.character(simsympt $ id))

head(simsympt)

##现在重要的东西

p < - ggplot (simsympt,aes(x = date,y = i d))
p = p + geom_tile(aes(fill = level))+
facet_grid(tx〜。,drop = T,space =free)+
scale_y_discrete(expand = c (0,0),drop = T)
p



我需要的是删除行之间的所有垂直空间在顶部和底部图(facet)中。例如,由于ID号15在对照组中,治疗组中不应该有她的行。
谢谢,
Seth

解决方案

  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 =水平))+
facet_grid(tx〜。,drop = T,space =free,scales =free)+
scale_y_discrete(expand = c(0,0),drop = T )


I am trying to make a faceted ggplot sorted by two discrete variables on the x axis. The problem is that I would like to have the vertically adjacent entries all touching. Currently there is space between rows based on which levels of the factor are in the top plot vs bottom. sorry this reproducible example is a bit verbose.

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

All I need is to remove the all the vertical space between rows in both the top and the bottom graph(facet). For example, since id number 15 is in the control group there should not be a row for her in the treatment group. Thanks, Seth

解决方案

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

EDIT: after further clarification

Removed the wrong space. What you want is to change your facet_grid call to include a scales="free" argument.

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)

这篇关于ggplot带有构面的geom_tile间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆