Dear R list members I am trying to improve a boxplot with 2 data sets. I run somethinkg like boxplot(data1 ~ month, add=F, col = "red", ...) boxplot(data2 ~ month, add=T, col = "blue", ...) The problem is that the data from February are missing for data2, so R think that must take little more space between the data classes in data 2 and then both data gropus are not aligned. In the R documentation I do not find any posile solution for tell R that I want reserve a extra space for this month, So the boxplot is erroneous and the x axis have a strange numeration 1 2 3 4 54 65 76 ... etc I would be grateful if anyone could suggest how to tell R That the boxplots must be fitted to the same x class in both data groups. To make a false data group with a 0 valor for february is not aceptable in this case Sincerely Pablo Valdes
Merge both datasets into one with a grouping variable. Then have a look at the ggplot2 package. library(ggplot2) dataset <- data.frame(Month = factor(rep(1:12, 20)), Value c(rnorm(120) + 1:12, rnorm(120) + 12:1), Group = gl(2, 120, labels LETTERS[1:2])) ggplot(dataset, aes(x = Month, y = Value, colour = Group)) + geom_boxplot() Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op r-project.org > [mailto:r-help-bounces op r-project.org] Namens p.valdes op arsystel.com > Verzonden: donderdag 4 oktober 2007 17:40 > Aan: r-help op r-project.org > Onderwerp: [R] A rebel boxplot question > > Dear R list members > I am trying to improve a boxplot with 2 data sets. I run > somethinkg like > > boxplot(data1 ~ month, add=F, col = "red", ...) > boxplot(data2 ~ month, add=T, col = "blue", ...) > > The problem is that the data from February are missing for > data2, so R think that must take little more space between > the data classes in data 2 and then both data gropus are not > aligned. In the R documentation I do not find any posile > solution for tell R that I want reserve a extra space for > this month, So the boxplot is erroneous and the x axis have a > strange numeration > > 1 2 3 4 54 65 76 ... etc > > I would be grateful if anyone could suggest how to tell R > That the boxplots must be fitted to the same x class in both > data groups. To make a false data group with a 0 valor for > february is not aceptable in this case > > Sincerely > Pablo Valdes > > ______________________________________________ > R-help op 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. >
I've altered the code slightly so one group has no data in one month. library(ggplot2) dataset <- data.frame(Month = factor(rep(1:12, 20)), Value c(rnorm(120) + 1:12, rnorm(120) + 12:1), Group = gl(2, 120, labels LETTERS[1:2])) dataset <- dataset[dataset$Month != 2 | dataset$Group != "A", ] ggplot(dataset, aes(x = Month, y = Value, colour = Group)) + geom_boxplot() Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: ONKELINX, Thierry > Verzonden: donderdag 4 oktober 2007 17:55 > Aan: 'p.valdes op arsystel.com'; r-help op r-project.org > Onderwerp: RE: [R] A rebel boxplot question > > Merge both datasets into one with a grouping variable. Then > have a look at the ggplot2 package. > > library(ggplot2) > dataset <- data.frame(Month = factor(rep(1:12, 20)), Value > = c(rnorm(120) + 1:12, rnorm(120) + 12:1), Group = gl(2, 120, > labels = LETTERS[1:2])) > ggplot(dataset, aes(x = Month, y = Value, colour = Group)) > + geom_boxplot() > > Cheers, > > Thierry > -------------------------------------------------------------- > -------------- > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek / Research Institute > for Nature and Forest Cel biometrie, methodologie en > kwaliteitszorg / Section biometrics, methodology and quality > assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 > 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be > > Do not put your faith in what statistics say until you have > carefully considered what they do not say. ~William W. Watt > A statistical analysis, properly conducted, is a delicate > dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney > > > > > -----Oorspronkelijk bericht----- > > Van: r-help-bounces op r-project.org > > [mailto:r-help-bounces op r-project.org] Namens p.valdes op arsystel.com > > Verzonden: donderdag 4 oktober 2007 17:40 > > Aan: r-help op r-project.org > > Onderwerp: [R] A rebel boxplot question > > > > Dear R list members > > I am trying to improve a boxplot with 2 data sets. I run somethinkg > > like > > > > boxplot(data1 ~ month, add=F, col = "red", ...) > > boxplot(data2 ~ month, add=T, col = "blue", ...) > > > > The problem is that the data from February are missing for > data2, so R > > think that must take little more space between the data classes in > > data 2 and then both data gropus are not aligned. In the R > > documentation I do not find any posile solution for tell R > that I want > > reserve a extra space for this month, So the boxplot is > erroneous and > > the x axis have a strange numeration > > > > 1 2 3 4 54 65 76 ... etc > > > > I would be grateful if anyone could suggest how to tell R That the > > boxplots must be fitted to the same x class in both data groups. To > > make a false data group with a 0 valor for february is not > aceptable > > in this case > > > > Sincerely > > Pablo Valdes > > > > ______________________________________________ > > R-help op 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. > >
On 5/10/2007, at 3:39 AM, <p.valdes at arsystel.com> wrote:> Dear R list members > I am trying to improve a boxplot with 2 data sets. I run somethinkg > like > > boxplot(data1 ~ month, add=F, col = "red", ...) > boxplot(data2 ~ month, add=T, col = "blue", ...) > > The problem is that the data from February are missing for data2, so R > think that must take little more space between the data classes in > data 2 > and then both data gropus are not aligned. In the R documentation I > do not > find any posile solution for tell R that I want reserve a extra > space for > this month, So the boxplot is erroneous and the x axis have a strange > numeration > > 1 2 3 4 54 65 76 ... etc > > I would be grateful if anyone could suggest how to tell R That the > boxplots must be fitted to the same x class in both data groups. To > make > a false data group with a 0 valor for february is not aceptable in > this > caseYour example is a bit confusing --- it must be ***month*** that February is missing from, not data2!!! --- but basically if you make month into a ***factor*** with levels equal to the complete set of months, then boxplot() will leave a gap for any month that is missing. E.g. clyde <- rnorm(100) melvin <- sample(letters[1:4],100,TRUE) melvin[melvin=="b"] <- "c" melvin <- factor(melvin,levels=letters[1:4]) boxplot(clyde ~ melvin) leaves a gap (an empty boxplot as it were) for b, and labels the x axis with "a", "b", "c", and "d". BTW --- ``add'' and ``col'' do not seem to be arguments for boxplot(). cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
Thanks to all for the answers ----------- Thierry : a very interesting answer... but I'm still trying to figure how to install ggplot2 :-) install.packages("ggplot") Warning in install.packages("ggplot2") : argument 'lib' is missing: using '...R/i486-pc-linux-gnu-library/2.5' ?? also installing the dependency 'colorspace' OK .... .gzip: /tmp/Rtmpl8LWFh/downloaded_packages/ggplot2_0.5.5.tar.gz: unexpected end of file tar: Unexpected EOF in archive tar: Unexpected EOF in archive tar: Error is not recoverable: exiting now .. 3: installation of package 'ggplot2' had non-zero exit status in: install.packages("ggplot2") -------------------------------- R.version() says: arch i486 os linux-gnu system i486, linux-gnu status major 2 minor 5.1 year 2007 month 06 day 27 svn rev 42083 language R version.string R version 2.5.1 (2007-06-27) My OS is a Linux Debian Sid ----------- S. Ellison wrote >> specifying at= for both plots might help. Ellison, I take a look to the opcion "at" but I can not understand reading the documentation how to use this option. for instance: month <-c( 1, 2, 3, 4, 5, 6, 7,8,9,10,11,12) y <-c( 1, 1, 1, 4, 4, 4, 4,4,1, 3, 3, 3) z <-c( 1, 3, 4, 5, 6, 7,8,9,10,11,12) boxplot(y ~ month, add=F, at= 1:12) #OK, now lets try to overlap the second plot boxplot(y ~ month, add=T, at= 1:12) #Error in model.frame(... variable lengths differ (found for 'month') boxplot(y ~ month, add=T, at= 1:11) #Error in model.frame... variable lengths differ (found for 'month') boxplot(y ~ month, add=T, at= 1,3,4,5,6,7,8,9,10,11,12) #Error in model.frame... variable lengths differ... boxplot(y ~ month, add=T, atc(1,3,4,5,6,7,8,9,10,11,12)) #Error in model.frame... variable lengths differ... ??? Another try 1/12 = 0.08333333 boxplot(y ~ month, add=T, at= 0.0833333) #Error in bxp(list(stats c(20, 20, 20, 20, 20, 0, 0, 0, 0, 0, 20, 20, : 'at' must have same length as 'z$n', i.e. 12 boxplot(y ~ month, add=T, at= 12) #same error ... 'at' must have same length as 'z$n', i.e. 12 Glups. :-} And now what? Pablo Valdes>>>> <p.valdes at arsystel.com> 04/10/2007 16:39:41 >>> > Dear R list members > I am trying to improve a boxplot with 2 data sets. I run somethinkg > like > > boxplot(data1 ~ month, add=F, col = "red", ...) > boxplot(data2 ~ month, add=T, col = "blue", ...) > > The problem is that the data from February are missing for data2, so R > think that must take little more space between the data classes in > data 2 and then both data gropus are not aligned. In the R > documentation I do not find any posile solution for tell R that I want > reserve a extra space for this month, So the boxplot is erroneous and > the x axis have a strange numeration > > 1 2 3 4 54 65 76 ... etc > > I would be grateful if anyone could suggest how to tell R That the > boxplots must be fitted to the same x class in both data groups. To > make a false data group with a 0 valor for february is not aceptable > in this case > > Sincerely > Pablo Valdes > > ______________________________________________ > 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. > > ******************************************************************* > This email contains information which may be confidential and/or > privileged, and is intended only for the individual(s) or > organisation(s) named above. If you are not the intended recipient, > then please note that any disclosure, copying, distribution or use of > the contents of this email is prohibited. Internet communications are > not 100% secure and therefore we ask that you acknowledge this. If you > have received this email in error, please notify the sender or contact > +44(0)20 8943 7000 or postmaster at lgcforensics.com immediately, and > delete this email and any attachments and copies from your system. > Thank you.> LGC Limited. Registered in England 2991879. > Registered office: Queens Road, Teddington, Middlesex TW11 0LY, UK
Rolf Turner wrote>Your example is a bit confusing ---Sorry, maybe I can provide a better example above of what I want.> ``add'' and ``col'' do not seem to be arguments for boxplot().Are you sure? ;-) Maybe you could want to take a look to this: clyde <- rnorm(100) melvin <- sample(letters[1:4],100,TRUE) melvin[melvin=="b"] <- "c" melvin <- factor(melvin,levels=letters[1:4]) boxplot((clyde+2) ~ melvin,add=FALSE,col=c("gray20","white","gray60","gray90")) boxplot((clyde/5) ~ melvin,add=TRUE,col=c("red","blue","yellow","lavender")) I'm intending to do something like this (but less psychodelic, only two colors :-) , and with a single boxplot in b) I am sudying also your idea, Thanks Rolf. Sincerely Pablo Valdes