Luigi Marongiu
2014-May-12 17:50 UTC
[R] package to plot mean and standard deviation directly on data
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)
Jim Lemon
2014-May-12 21:36 UTC
[R] package to plot mean and standard deviation directly on data
On Mon, 12 May 2014 06:50:29 PM Luigi Marongiu wrote:> 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? moreor> 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 horizontalbars> 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) >Hi Luigi, Have a look at brkdn.plot in the plotrix package. Jim
Duncan Mackay
2014-May-13 00:13 UTC
[R] package to plot mean and standard deviation directly on data
Hi Luigi I have not got time at the moment to look at you question but see library(xyplot) demo(intervals) Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi Marongiu Sent: Tuesday, 13 May 2014 03:50 To: r-help at r-project.org Subject: [R] package to plot mean and standard deviation directly on data 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) ______________________________________________ R-help at r-project.org 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.