[R] package to plot mean and standard deviation directly on data

Luigi Marongiu marongiu.luigi at gmail.com
Mon May 12 19:50:29 CEST 2014


 Dear all,
is there a package, preferably not ggplot, to draw the lines for the
standard deviation just by providing the vector with the data? more or
less as it happens with the boxplot function.
the procedure i implemented herein requires the calculation of the
means, standard deviations, limits (mean+/-SD) and the horizontal bars
as well, everything plotted individually. the packages i have seen on
google they all require to calculate the mean/SD prior to the plot.
best wishes,
luigi


# in case you are interested this is the code, which is faulty for the
factor of the variable "sample"

my.data<-structure(list(
group = c(
0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
sample = c(
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1),
copy = c(
626.01, 584.37, 568.5, 154.76, 146.47, 155.83,
297.64, 291.56, 272.16, 414.19, 402.94, 402.94,
351.11, 274.04, 291.56, 66.82, 39.06, 83.87,
16.98, 45.76, 15, 1.39, 98.25, 13.62, 181.32,
142.49, 108.94, 4.95, 5.92, 4.72, 25.14, 31.12,
40.15, 34.27, 26.93, 24.8)),
 row.names = c(NA, -36L),  class = "data.frame")
# define subset
A<-subset(my.data, group==0)
# define average and standard deviation
avg.A<-aggregate(copy ~ sample, A, mean)
names(avg.A)<-c("samp.", "avg")
sd.A<-aggregate(copy ~ sample, A, sd)
names(sd.A)<-c("Samp", "sd")
A.bound<-merge(avg.A, sd.A, by.x="samp.", by.y="Samp")
# define limits
min.A<-A.bound$avg - A.bound$sd
max.A<-A.bound$avg + A.bound$sd
# plot
plot(avg  ~ samp., data=avg.A,  pch=16, ylim=c(-10, 600),
las=3, cex.lab=1.5, cex.main=1.8, col="black",
ylab=expression(bold("copy")),
xlab=expression(bold("sample")),
main="Mean and std dev")
# add limit segments
segments(seq(length(avg.A$sample)), max.A, seq(length(avg.A$sample)),  min.A)
segments(seq(length(avg.B$sample)), max.B, seq(length(avg.B$sample)),  min.B)
# add notch
notch<-0.02  # define end of bar extension
segments(seq(length(avg.A$sample))-notch, max.A,
seq(length(avg.A$sample))+notch,  max.A)
segments(seq(length(avg.A$sample))-notch, min.A,
seq(length(avg.A$sample))+notch,  min.A)



More information about the R-help mailing list