[R] boxplot - labelling

Chuck Cleland ccleland at optonline.net
Sat May 6 12:11:20 CEST 2006


Sachin J wrote:
> Hi,
>    
>   How can I get the values of mean and median (not only points but values too) on the boxplot. I am using boxplot function from graphics package. Following is my data set
>    
>   > df 
>   [1]  5  1  1  0  0 10 38 47  2  5  0 28  5  8 81 21 12  9  1 12  2  4 22  3
>    
>   > mean.val <- sapply(df,mean)
>> boxplot(df,las = 1,col = "light blue")
>> points(seq(df), mean.val, pch = 19)
>    
>   I could get mean as dot symbol but i need values too? Also how to print the x-axis labels vertically instead of horizontally? Is there any other function to achieve these?

A few ideas to try:

df <- c(5, 1, 1, 0, 0, 10, 38, 47, 2, 5, 0, 28, 5, 8, 81, 21, 12, 9, 1, 
12, 2, 4, 22, 3)
boxplot(df, las=1, col="light blue")
points(1, mean(df), pch=19)
text(0.75, mean(df), paste("Mean =", round(mean(df), 2)), adj=1)
text(1.25, median(df), paste("Median =", round(median(df), 2)), adj=0)

boxplot(df, las=1, col="light blue")
points(1, mean(df), pch = 19)
text(0.75, mean(df), round(mean(df), 2), adj=1)
text(1.25, median(df), round(median(df), 2), adj=0)

mydf <- data.frame(A=runif(100), B=runif(100), C=runif(100), D=runif(100))
par(mar=c(8,5,5,5))
boxplot(mydf, las=1, col="light blue")
points(seq(mydf), colMeans(mydf), pch=19)
mtext(side=1, line=2, at=seq(mydf), paste("Mean =", 
round(colMeans(mydf),2)))
mtext(side=1, line=3, at=seq(mydf), paste("Median =", round(apply(mydf, 
2, median),2)))

Also, see bp.example() in the following post:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/3158.html

-- 
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




More information about the R-help mailing list