Mächler Marc Jaques
2010-May-12 13:30 UTC
[R] Boxplot position on X-axis relative to it's value
Dear R-Experts. I collected different datas about Nitrogen content (mg/ml) in Dung. The dung was eighter fresh (day=0) or had different ages (15,29,47) to observe nutrient changes over time. Now I like to draw a boxplot. boxplot(nmgml~day) abline((nmgml~day) The Problem is, that the boxplot considers the day values as groups and not as time series (neighter when the days are numeric or factors). The result is, that the regression line looks different as when I draw a plot plot(nmgml~day) abline(nmgml~day) How can I reach that the boxplot's position on the x-axis is relative to the time value? (and the days are not just treated as groups) Data sample (Girafe-Dung) day nmgml 1 29 2.72655 2 29 2.48820 3 15 2.85945 4 15 2.58915 5 15 2.88345 6 15 2.66675 7 47 3.29125 8 15 2.44085 9 29 2.43065 10 29 2.43425 11 29 2.42705 12 0 1.12350 13 15 5.10310 14 15 0.99880 15 0 2.22350 16 47 2.18290 17 47 2.21570 18 47 2.46450 19 29 2.53350 20 47 2.78550 21 47 3.06920 22 0 2.65660 23 0 2.16650 24 0 2.20620 25 0 1.91870 26 0 2.45160 27 0 2.35980 28 0 1.99900 29 0 2.16300 30 15 2.00460 31 15 2.56220 32 47 2.38650 33 29 2.65820 34 29 2.47670 35 47 2.23770 36 29 2.40080 37 47 2.12650 38 29 2.35950 I really apreciate your help Sincerly... MJMaechler (Zurich, Switzerland)
RICHARD M. HEIBERGER
2010-May-12 17:57 UTC
[R] Boxplot position on X-axis relative to it's value
You can use the bwplot panel in the HH package. You might need to install HH first, by uncommenting the line below. girafe.txt <- textConnection( " day nmgml 1 29 2.72655 2 29 2.48820 3 15 2.85945 4 15 2.58915 5 15 2.88345 6 15 2.66675 7 47 3.29125 8 15 2.44085 9 29 2.43065 10 29 2.43425 11 29 2.42705 12 0 1.12350 13 15 5.10310 14 15 0.99880 15 0 2.22350 16 47 2.18290 17 47 2.21570 18 47 2.46450 19 29 2.53350 20 47 2.78550 21 47 3.06920 22 0 2.65660 23 0 2.16650 24 0 2.20620 25 0 1.91870 26 0 2.45160 27 0 2.35980 28 0 1.99900 29 0 2.16300 30 15 2.00460 31 15 2.56220 32 47 2.38650 33 29 2.65820 34 29 2.47670 35 47 2.23770 36 29 2.40080 37 47 2.12650 38 29 2.35950") girafe <- read.table(girafe.txt, header=TRUE) close.connection(girafe.txt) ## install.packages(HH) ## if you don't already have it library(HH) girafe$day <- factor(girafe$day) position(girafe$day) <- as.numeric(levels(girafe$day)) bwplot(nmgml ~ day, data=girafe, scales=list(x=list(limits=c(-5, 53), at=position(girafe$day), labels=position(girafe$day))), box.width=5, panel=panel.bwplot.intermediate.hh) There is a bug in the lattice installed with R-2.11.0 that makes it ignore the limits argument. A repair is on r-forge now and will be released soon. The limits argument works correctly with R-2.10.1. Another example is on my website http://astro.temple.edu/~rmh/HH/bwplot-color.pdf [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-May-12 18:12 UTC
[R] Boxplot position on X-axis relative to it's value
One option could be: # Using Richard's example boxplot(nmgml ~ factor(day, levels = do.call(seq, as.list(range(girafe$day)))), data = girafe, xaxt = 'n') axis(1, at = unique(girafe$day)) On Wed, May 12, 2010 at 10:30 AM, Mächler Marc Jaques <marcm@student.ethz.ch> wrote:> Dear R-Experts. > > I collected different datas about Nitrogen content (mg/ml) in Dung. The > dung was eighter fresh (day=0) or had different ages (15,29,47) to observe > nutrient changes over time. > > Now I like to draw a boxplot. > > boxplot(nmgml~day) > abline((nmgml~day) > > The Problem is, that the boxplot considers the day values as groups and not > as time series (neighter when the days are numeric or factors). > > The result is, that the regression line looks different as when I draw a > plot > plot(nmgml~day) > abline(nmgml~day) > > How can I reach that the boxplot's position on the x-axis is relative to > the time value? (and the days are not just treated as groups) > > Data sample (Girafe-Dung) > day nmgml > 1 29 2.72655 > 2 29 2.48820 > 3 15 2.85945 > 4 15 2.58915 > 5 15 2.88345 > 6 15 2.66675 > 7 47 3.29125 > 8 15 2.44085 > 9 29 2.43065 > 10 29 2.43425 > 11 29 2.42705 > 12 0 1.12350 > 13 15 5.10310 > 14 15 0.99880 > 15 0 2.22350 > 16 47 2.18290 > 17 47 2.21570 > 18 47 2.46450 > 19 29 2.53350 > 20 47 2.78550 > 21 47 3.06920 > 22 0 2.65660 > 23 0 2.16650 > 24 0 2.20620 > 25 0 1.91870 > 26 0 2.45160 > 27 0 2.35980 > 28 0 1.99900 > 29 0 2.16300 > 30 15 2.00460 > 31 15 2.56220 > 32 47 2.38650 > 33 29 2.65820 > 34 29 2.47670 > 35 47 2.23770 > 36 29 2.40080 > 37 47 2.12650 > 38 29 2.35950 > > I really apreciate your help > > Sincerly... > > MJMaechler (Zurich, Switzerland) > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi: Here are a couple of ways to superimpose the boxplots with a line, using lattice and latticeExtra. Notice that the boxplots are located at the observed x values - the idea comes straight out of the example on p. 183 of the Lattice book. library(lattice) library(latticeExtra) # Connect the medians xyplot(nmgml ~ day, data = girafe, panel = panel.bwplot, horizontal = FALSE) + layer(panel.average(..., fun = median, col.line = 'gray30')) # Least squares regression line xyplot(nmgml ~ day, data = girafe, panel = panel.bwplot, horizontal = FALSE) + layer(panel.lmline(...)) Caution: be careful if you have the ggplot2 package loaded, because the layer function in ggplot2 can mask the layer function in latticeExtra. ------------ Aside: I couldn't figure out how to get the horizontal = FALSE argument to work within a single panel function that contains both panel.bwplot and panel.average. I tried xyplot(nmgml ~ day, data = girafe, panel = function(...) { panel.bwplot(..., horizontal = FALSE) panel.average(..., fun = median, col.line = 'gray30') } ) and a couple of variants thereof but couldn't get it to work. If there's an easy solution, I'd be interested in seeing it. HTH, Dennis On Wed, May 12, 2010 at 6:30 AM, Mächler Marc Jaques <marcm@student.ethz.ch>wrote:> Dear R-Experts. > > I collected different datas about Nitrogen content (mg/ml) in Dung. The > dung was eighter fresh (day=0) or had different ages (15,29,47) to observe > nutrient changes over time. > > Now I like to draw a boxplot. > > boxplot(nmgml~day) > abline((nmgml~day) > > The Problem is, that the boxplot considers the day values as groups and not > as time series (neighter when the days are numeric or factors). > > The result is, that the regression line looks different as when I draw a > plot > plot(nmgml~day) > abline(nmgml~day) > > How can I reach that the boxplot's position on the x-axis is relative to > the time value? (and the days are not just treated as groups) > > Data sample (Girafe-Dung) > day nmgml > 1 29 2.72655 > 2 29 2.48820 > 3 15 2.85945 > 4 15 2.58915 > 5 15 2.88345 > 6 15 2.66675 > 7 47 3.29125 > 8 15 2.44085 > 9 29 2.43065 > 10 29 2.43425 > 11 29 2.42705 > 12 0 1.12350 > 13 15 5.10310 > 14 15 0.99880 > 15 0 2.22350 > 16 47 2.18290 > 17 47 2.21570 > 18 47 2.46450 > 19 29 2.53350 > 20 47 2.78550 > 21 47 3.06920 > 22 0 2.65660 > 23 0 2.16650 > 24 0 2.20620 > 25 0 1.91870 > 26 0 2.45160 > 27 0 2.35980 > 28 0 1.99900 > 29 0 2.16300 > 30 15 2.00460 > 31 15 2.56220 > 32 47 2.38650 > 33 29 2.65820 > 34 29 2.47670 > 35 47 2.23770 > 36 29 2.40080 > 37 47 2.12650 > 38 29 2.35950 > > I really apreciate your help > > Sincerly... > > MJMaechler (Zurich, Switzerland) > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]