Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

r - Remove blank lines from plot geom_tile ggplot

Is there a way to remove the blank lines from a geom_tile plot? If I try scale = "free" it looks weird. I want to preserve the width and height of each facet. Here is my code:

 data.m <- melt(data, id.vars=c("Group","miRNA"))
 head(data.m)
 #    Group           miRNA  variable     value
 #  1 Serum   hsa-let-7d-5p 1_0h_rep1 0.5356346
 #  2 Serum   hsa-let-7e-5p 1_0h_rep1 0.5994119
 #  3 Serum   hsa-let-7g-5p 1_0h_rep1 0.5097298
 #  4 Serum hsa-miR-106a-5p 1_0h_rep1 0.3566843
 #  5 Serum hsa-miR-1180-3p 1_0h_rep1 0.6019190
 #  6 Serum  hsa-miR-152-5p 1_0h_rep1 0.1815842


 p <- ggplot(data.m, aes(variable, miRNA))
 p  + geom_tile(aes(fill = value)) + facet_wrap(~Group,ncol=1) +
scale_fill_gradient2(low=muted("blue"), high=muted("red")) +
scale_x_discrete(labels=c("0h_rep2", "0h_rep1", "1h_rep2","1h_rep1","4h_rep2","4h_rep1","9h_rep2","9h_rep1", "15h_rep2", "15h_rep1","18h_rep2","18h_rep1","21h_rep2","21h_rep1")) + 
  theme_bw(base_size=20) + 
  theme(axis.text.x=element_text(angle=0, vjust=0.5, hjust=0, size=12), axis.text.y=element_text(size=12), strip.text.y=element_text(angle=0, vjust=0.5, hjust=0.5, size=12),
    strip.text.x=element_text(size=12)) + 
  labs(y="Genes", x="Time (h)", fill="")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should add argument scales="free_y" to facet_wrap() to remove "empty" lines.

 +facet_wrap(~Group,ncol=1,scales="free_y") 

If you need to have the same width of tiles after removing the "empty" lines then use facet_grid() instead of facet_wrap() with argument space="free".

 +facet_grid(Group~.,scales="free_y",space="free") 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...