YQ.Wang YQ.Wang - 2 months ago 16
R Question

combine plots with different variable relations

The

df
has three normal variables:
a
,
b
,
c
and one group variable:
d
.

df<-data.frame(c(5,1,1,2,3,7,5,4,2,1),c(1:10),c(1,1,1,1,1,2,1,2,1,2),c(rep('o',5),rep('m',5)))
colnames(df)<-c('a','b','c','d')


Considering these two plots:


  1. a-b plot grouped by d:
    qplot(a,b,data=df,geom = 'point')+facet_wrap(~d,nrow = 2)

  2. b-c plot grouped by d:
    qplot(b,c,data=df,geom = 'point')+facet_wrap(~d,nrow = 2)



Is it possible to combine these two plots into one plot using R?

Answer

Do you want somethign like the following?

library(grid)
library(gridExtra)
grid.arrange(qplot(a,b,data=df,geom = 'point')+facet_wrap(~d,nrow = 2),
             qplot(b,c,data=df,geom = 'point')+facet_wrap(~d,nrow = 2),
             ncol=2)

enter image description here