Hi everyone. I have two data frames that contain the same variables but with different number of observation. I would like to know it was possible to combine the data so I can have "paired" boxplot on the same figure. For example, df1 = data.frame(x = rnorm(100)) df1$type = ifelse(df1$x <= 0 , "type1", "type2") df2 = data.frame(x = rnorm(50,0,2)) df2$type = ifelse(df2$x <= 0 , "type1", "type2") ## How to combine boxplot boxplot(df1$x~df1$type) boxplot(df2$x~df2$type) df1 would be observed data whereas df2 would be simulated data. I would like to have the two categories (type1 and type2) on x axis and a colour to differentiate simulated vs observed datra. Regards, Phil
Does the following produce what you want?
> lst <- c(Observed=with(df1, split(x, type)), Simulated=with(df2,
split(x, type)))
> lst <- lst[c(seq(1,length(lst)-1,by=2), seq(2,length(lst),by=2))]
> boxplot(lst,
col=rep(c("gray","red"),len=length(lst)))
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf
> Of Philippe Massicotte
> Sent: Monday, February 25, 2013 8:04 AM
> To: r-help at r-project.org
> Subject: [R] Combining boxplot
>
> Hi everyone.
>
> I have two data frames that contain the same variables but with different
> number of observation.
>
> I would like to know it was possible to combine the data so I can have
> "paired" boxplot on the same figure.
>
> For example,
>
> df1 = data.frame(x = rnorm(100))
> df1$type = ifelse(df1$x <= 0 , "type1", "type2")
>
> df2 = data.frame(x = rnorm(50,0,2))
> df2$type = ifelse(df2$x <= 0 , "type1", "type2")
>
> ## How to combine boxplot
> boxplot(df1$x~df1$type)
> boxplot(df2$x~df2$type)
>
> df1 would be observed data whereas df2 would be simulated data.
>
> I would like to have the two categories (type1 and type2) on x axis and a
> colour to differentiate simulated vs observed datra.
>
> Regards,
> Phil
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
does this work for you? df1 = data.frame(x = rnorm(100)) df1$type = ifelse(df1$x <= 0 , "type1", "type2") df1$group<-1 df2 = data.frame(x = rnorm(50,0,2)) df2$type = ifelse(df2$x <= 0 , "type1", "type2") df2$group<-2 combined.df<-rbind(df1,df2) boxplot(combined.df$x ~ combined.df$group *combined.df$type) Filoche wrote> Hi everyone. > > I have two data frames that contain the same variables but with different > number of observation. > > I would like to know it was possible to combine the data so I can have > "paired" boxplot on the same figure. > > For example, > > df1 = data.frame(x = rnorm(100)) > df1$type = ifelse(df1$x <= 0 , "type1", "type2") > > df2 = data.frame(x = rnorm(50,0,2)) > df2$type = ifelse(df2$x <= 0 , "type1", "type2") > > ## How to combine boxplot > boxplot(df1$x~df1$type) > boxplot(df2$x~df2$type) > > df1 would be observed data whereas df2 would be simulated data. > > I would like to have the two categories (type1 and type2) on x axis and a > colour to differentiate simulated vs observed datra. > > Regards, > Phil-- View this message in context: http://r.789695.n4.nabble.com/Combining-boxplot-tp4659603p4659631.html Sent from the R help mailing list archive at Nabble.com.