I am very new to R so forgive me if this seems basic but I have done extensive searching and failed to come up with the answer for myself. I am trying to label a boxplot I have created with the values for the median, upper and lower quartiles and max and min values. I have been unable to do this or find anything on the net to say how it might be done. Is this possible and if so how? Regards, Daniel Siddle
Daniel Siddle wrote:> I am very new to R so forgive me if this seems basic but I have done extensive searching and failed to come up with the answer for myself. > > I am trying to label a boxplot I have created with the values for the median, upper and lower quartiles and max and min values. I have been unable to do this or find anything on the net to say how it might be done. Is this possible and if so how? Regards,This message from back in 2002 gives a function called bp.example(), which shows how a boxplot might be annotated: http://tolstoy.newcastle.edu.au/R/help/02a/1515.html You could easily modify it into a stripped down version that does what you want.> Daniel Siddle > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Daniel Siddle wrote:> I am very new to R so forgive me if this seems basic but I have done extensive searching and failed to come up with the answer for myself. > > I am trying to label a boxplot I have created with the values for the median, upper and lower quartiles and max and min values. I have been unable to do this or find anything on the net to say how it might be done. Is this possible and if so how? Regards,Here is another idea: fn <- boxplot(ToothGrowth$len, plot=FALSE)$stats par(mar=c(4,6,4,2)) boxplot(ToothGrowth$len, ylab="Length", at=.80) text(1.15, fn[1], paste("Minimum Value =", fn[1]), adj=0, cex=.7) text(1.15, fn[2], paste("Lower Quartile =", fn[2]), adj=0, cex=.7) text(1.15, fn[3], paste("Median =", fn[3]), adj=0, cex=.7) text(1.15, fn[4], paste("Upper Quartile =", fn[4]), adj=0, cex=.7) text(1.15, fn[5], paste("Maximum Value =", fn[5]), adj=0, cex=.7) arrows(1.14, fn[1], 1.02, fn[1]) arrows(1.14, fn[2], 1.02, fn[2]) arrows(1.14, fn[3], 1.02, fn[3]) arrows(1.14, fn[4], 1.02, fn[4]) arrows(1.14, fn[5], 1.02, fn[5]) title("Annotated Boxplot of Tooth Growth")> Daniel Siddle > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Here is one way of labeling the values: # capture data returned by boxplot x <- boxplot(count ~ spray, data = InsectSprays, col = "lightgray") # plot each group for (i in seq(ncol(x$stats))){ text(i, x$stats[,i], labels=x$stats[,i]) } # if there are outliers, plot them if (length(x$out) > 0){ # split the groups so you can get max/min maxmin <- split(x$out, x$group) # go through each group getting min/max lapply(names(maxmin), function(.grp){ .range <- range(maxmin[[.grp]]) text(as.numeric(.grp), .range, labels=.range) }) } On 4/6/07, Daniel Siddle <sidds13 at hotmail.com> wrote:> > I am very new to R so forgive me if this seems basic but I have done extensive searching and failed to come up with the answer for myself. > > I am trying to label a boxplot I have created with the values for the median, upper and lower quartiles and max and min values. I have been unable to do this or find anything on the net to say how it might be done. Is this possible and if so how? Regards, > > Daniel Siddle > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Just wanted to say thanks very much. I used Chuck's 2nd idea as I found it the easiest to understand as I'm still finding me feet with R. Just for reference for anyone else, fn[1] and fn[5] actually pasted and gave the values at the whiskers (~1.5 IQR) so replaced them with a max and min function which returned the true max and min values and pasted at the correct heights. Thanks once again for all the help. Regards, Daniel Siddle
A function that uses ggplot package to make annotated boxplots is described at http://vikasrawal.wordpress.com/2007/02/25/working-with-ggplot/ An advantage of using ggplot is that you can plot weighted boxplots. Vikas Rawal Associate Professor Centre for Economic Studies and Planning Jawaharlal Nehru University, New Delhi