[R] Displaying median value over the horizontal(median)line in the boxplot

arun smartpink111 at yahoo.com
Thu Mar 21 21:19:45 CET 2013


Hi,
set.seed(45)
test1<-data.frame(columnA=rnorm(7,45),columnB=rnorm(7,10)) #used an example probably similar to your actual data
apply(test1,2,function(x) sprintf("%.1f",median(x)))
#columnA columnB 
# "44.5"  "10.2" 
par(mfrow=c(1,2))
lapply(test1,function(x) {b<- boxplot(x,range=0,horizontal=TRUE);mtext(sprintf("%.1f",b$stats[3]),side=3,at=b$stats[3],line=-8)})
A.K.



----- Original Message -----
From: "shanthimulinti at gmail.com" <shanthimulinti at gmail.com>
To: smartpink111 at yahoo.com
Cc: 
Sent: Thursday, March 21, 2013 4:05 PM
Subject: Re: Displaying median value over the horizontal(median)line in the boxplot

Here is what I am doing
my=read.csv("test.csv")
apply(my,2,median,na.rm=TRUE)
b<-boxplot(my,range=0, pars=list(boxwex=0.6))
After this I am not sure how to automatically list the median in each of the two boxplots

test.csv has
columnA  columnB
1                 1        
2                  2
3                  3
4                 4
5                 5
6                6
7                7

Finally how do I control the output. My actual data has a median value of 7.642552 but I only want it to be displayed as 7.6.

Thank you so much for your help. I am very sorry for troubling you but I am very very new to this programming and to R as you can see.



<quote author='arun kirshna'>
Hi,
Lines1<-readLines(textConnection("Column1 -1,2,3,4,5,6,6,7
COlumn2- 3,4,5,6,7,8,8
Column3-- 45,66,7,8,89,
COlumn4-5,6,7,7,8,9
Column5 -5,6,7,8,8")) 
vec1<-unlist(strsplit(Lines1,"-"))

dat1<-as.data.frame(t(read.table(text=vec1[grepl(",",vec1)],sep=",",fill=TRUE)))
row.names(dat1)<-NULL
colnames(dat1)<-tolower(gsub(" $","",vec1[grepl("^C",vec1)]))
dat1
#  column1 column2 column3 column4 column5
#1       1       3      45       5       5
#2       2       4      66       6       6
#3       3       5       7       7       7
#4       4       6       8       7       8
#5       5       7      89       8       8
#6       6       8      NA       9      NA
#7       6       8      NA      NA      NA
#8       7      NA      NA      NA      NA
apply(dat1,2,median,na.rm=TRUE)
#column1 column2 column3 column4 column5 
#    4.5     6.0    45.0     7.0     7.0 

#to save it as a pdf
pdf("Shanthiboxplot.pdf")
par(mfrow=c(3,2))
lapply(dat1,function(x)
{b<-boxplot(x,range=0,horizontal=TRUE);mtext(b$stats[3],side=3,at=b$stats[3],line=-8)})
dev.off()

#to just print it on screen
par(mfrow=c(3,2))# you can change this according to your need
lapply(dat1,function(x)
{b<-boxplot(x,range=0,horizontal=TRUE);mtext(b$stats[3],side=3,at=b$stats[3],line=-8)})
A.K.
</quote>
Quoted from: 
http://r.789695.n4.nabble.com/Displaying-median-value-over-the-horizontal-median-line-in-the-boxplot-tp4661854p4661935.html




More information about the R-help mailing list