I have some data consisting of multiple trials of an experiment with different values of an independent variable. If I run R> plot(var,result) I get a scatterplot of result versus the independent variable var. If I run R> plot(as.factor(var),result) I get a boxplot of the distribution of result for each value of var. In this plot, each boxplot is labeled by the corresponding value of var, but the absissas are evenly spaced. Is it possible to generate a boxplot with the absissas of each boxplot equal to the corresponding value of var? -- Alan Barnett, PhD Imaging Physicist National Institutes of Health NIMH/CBDB 301 402 3507
On Fri, 2007-12-14 at 08:16 -0500, Alan Barnett wrote:> I have some data consisting of multiple trials of an experiment with > different values of an independent variable. If I run > R> plot(var,result) > I get a scatterplot of result versus the independent variable var. > If I run > R> plot(as.factor(var),result) > I get a boxplot of the distribution of result for each value of var. In > this plot, each boxplot is labeled by the corresponding value of var, > but the absissas are evenly spaced. > Is it possible to generate a boxplot with the absissas of each boxplot > equal to the corresponding value of var?If you want boxplots, you could do something like this: CV <- rnorm(100) FV <- sample(c(1, 3, 6), 100, replace = TRUE) boxplot(CV ~ factor(FV, levels = seq(max(FV)))) This essentially 'fills in' the missing values (levels) of the factor variable so that they are included in the plot. HTH, Marc Schwartz
Hi Alan, Yes it is, but you need to do a bit work. There are different approaches. Look at the at= option under ?bxp and draw your boxplots with something like: boxplot(y ~ as.numeric(as.factor(grp)), at=c(0.5, 2, 2.5, 3), xaxt="n", ...) axis(side=1, at=c(0.5, 2, 2.5, 3), labels=c("0.5","2","2.5","3")) This should do it, and is perhaps the easiest route. There may be something missing, because I don't have a concrete example. Anyhow, you should be able to fill in the gaps. HTH, Mark. Alan Barnett wrote:> > I have some data consisting of multiple trials of an experiment with > different values of an independent variable. If I run > R> plot(var,result) > I get a scatterplot of result versus the independent variable var. > If I run > R> plot(as.factor(var),result) > I get a boxplot of the distribution of result for each value of var. In > this plot, each boxplot is labeled by the corresponding value of var, > but the absissas are evenly spaced. > Is it possible to generate a boxplot with the absissas of each boxplot > equal to the corresponding value of var? > > -- > Alan Barnett, PhD > Imaging Physicist > National Institutes of Health > NIMH/CBDB > 301 402 3507 > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Plot-question-tp14335709p14336622.html Sent from the R help mailing list archive at Nabble.com.
The easiest way would be to use the HH package, which you can get from CRAN. Marc's example is limited to integers on the X axis. Using positioned(), which is an extension to ordered(), allows arbitrary values. Building on that example require(HH) CV <- rnorm(100) FV <- sample(c(-1.4, 3.2, 5), 100, replace = TRUE) bwplot(CV ~ positioned(FV)) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alan Barnett Sent: Friday, December 14, 2007 08:16 AM To: R-Help Subject: [R] Plot question I have some data consisting of multiple trials of an experiment with different values of an independent variable. If I run R> plot(var,result) I get a scatterplot of result versus the independent variable var. If I run R> plot(as.factor(var),result) I get a boxplot of the distribution of result for each value of var. In this plot, each boxplot is labeled by the corresponding value of var, but the absissas are evenly spaced. Is it possible to generate a boxplot with the absissas of each boxplot equal to the corresponding value of var? -- Alan Barnett, PhD Imaging Physicist National Institutes of Health NIMH/CBDB 301 402 3507 ______________________________________________ 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.