[R] BoxPlot Adding Mean and Median Values

S Ellison S@E|||@on @end|ng |rom LGCGroup@com
Thu May 10 16:12:10 CEST 2018


> Here is the code I have tried.
The code has to work to be useful.
including the data read - which, because it is on your D: drive, clearly can't be read..

But here are some comments and some base graphics code that should get you started;

> As I mentioned below, would like to add the data labels into each box of mean
> value
	#You'll need to calculate the mean values separately. A standard boxplot doesn't include them

> AmbientTr <- read.csv("AmbientBatchbox.csv", stringsAsFactors = TRUE)
	#Not possible

> str(AmbientTr)
> summary(AmbientTr)
	#Not possible
> install.packages("ggplot2")
	#Don;t need to do this every time
> library(ggplot2)
	#And you don’t need it at all: you are using 'boxplot', which is in base graphics

> boxplot(RTF~Batch,data=AmbientTr, ylim = c(0,30), main="RTF By Batch",
> xlab="Batch", ylab="RTF", col ="blue")
	#Try assigning the result of boxplot to a variable, such as
bw.data <-  boxplot(RTF~Batch,data=AmbientTr, ylim = c(0,30), main="RTF By Batch",
	 xlab="Batch", ylab="RTF", col ="blue")

#Then you csan use that data to place labels etc. because boxplot returns the stats for the boxes, and the locations 
#are (by default) at 1:length()bw.data$names.

#For example, after the above plot:
with(bw.data, text(1:length(names), stats[3,], names, pos=1) #labels just below the medians

#And for placing means on the plot:
RTFmeans <- with(AmbientTr, tapply(RTF, batch, mean))
points(1:length(RTFmeans), RTFmeans, pch=19)
text(1:length(RTFmeans), RTFmeans, paste(round(RTFmeans,0)), pos=3) #labels just above the points 

#... or something like that. 

For a ggplot equivalent, you could try the ggplot mailing list; Google it to find it.

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmaster using lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK


More information about the R-help mailing list