2016-06-01 11 views
0

Rでggplot2を使ってそのようなプロットをしようとしていますか?下図のパネルg、h j、l、mを確認してください。したがって、メインプロットの隣にボックスプロットがある散布図。 http://www.ncbi.nlm.nih.gov/pubmed/26437030x軸とy軸に隣接するボックスプロットを持つR散布図

は、ここに私の解決策だあなたに enter image description here

+1

一部のデータとあなたが動作しませんでした正確に何を一緒にしようとしたコードを投稿してください。 – beetroot

+0

私のエラーが見つかりました。グリッドパッケージに問題がありました。私は答えとして私の機能を掲示します。 –

+1

@NicoBxl cowplotはこれをうまく行います。あなたの答えからプロットを使う。 ggdraw()+ draw_plot(p1,0.2,0、width = 0.8、height = 0.8)+ draw_plot(p2,0,0、width = 0.2、height = 0.8)+ draw_plot(p3,0.2,0.8、幅= 0.8、高さ= 0.2) – timcdlucas

答えて

2

ありがとう:FYI図は、この記事からです

...私はここで説明する方法を試みたが、それはすべてのhttp://www.r-bloggers.com/scatterplot-with-marginal-boxplots/では動作しませんでした。私はプロットの左側にy boxplotを配置する方法を知りませんし、右側には配置しません。私はgtable_add_grobパラメータを変更する必要があると思います。

library(ggplot2) 
library(gtable) 
library(grid) 

a.x <- rnorm(20,5,1) 
a.y <- rnorm(20,10,2) 
b.x <- rnorm(10,20,2) 
b.y <- rnorm(10,5,2) 
x <- data.frame(x=c(a.x,b.x),y=c(a.y,b.y),col=c(rep("A",20),rep("B",10))) 

p1 <- ggplot(x,aes(x=x,y=y))+geom_point(aes(color=col))+stat_smooth(method = "lm",se=F,colour="black",linetype=2,size=0.5)+theme_bw()+theme(legend.position="none") 
p2 <- ggplot(x,aes(x=col,y=y,color=col))+geom_boxplot()+theme_bw()+theme(legend.position="none",axis.ticks.y=element_blank(),axis.title.y=element_blank(),axis.text.y=element_blank()) 
p3 <- ggplot(x,aes(x=col,y=x,color=col))+geom_boxplot()+coord_flip()+theme_bw()+theme(legend.position="none",axis.ticks.x=element_blank(),axis.title.x=element_blank(),axis.text.x=element_blank()) 

gt1 <- ggplot_gtable(ggplot_build(p1)) 
gt2 <- ggplot_gtable(ggplot_build(p2)) 
gt3 <- ggplot_gtable(ggplot_build(p3)) 

maxWidth <- unit.pmax(gt1$widths[2:3], gt2$widths[2:3]) 
maxHeight <- unit.pmax(gt1$heights[4:5], gt3$heights[4:5]) 


gt1$widths[2:3] <- as.list(maxWidth) 
gt2$widths[2:3] <- as.list(maxWidth) 

gt1$heights[4:5] <- as.list(maxHeight) 
gt3$heights[4:5] <- as.list(maxHeight) 

gt <- gtable(widths = unit(c(4, 1), "null"), height = unit(c(1, 4), "null")) 

gt <- gtable_add_grob(gt, gt1, 2, 1) 
gt <- gtable_add_grob(gt, gt2, 2, 2) 
gt <- gtable_add_grob(gt, gt3, 1, 1) 


grid.newpage() 
grid.draw(gt) 

そして結果:

enter image description here

関連する問題