[R] How to plot doubles series with different location using plotCI

Jim Lemon jim at bitwrit.com.au
Mon Feb 11 11:41:16 CET 2013


On 02/11/2013 08:01 PM, linde wrote:
>
>
> Dear list
> members,
>
> I would
> like to create two series of plotted mean values and error bars, yet with
> different locations along the x-axis.
> Plotting of
> first series using plotCI with the standard arguments goes without any problem.
> However I do not succeed to add the second series in the same plot, which
> should be horizontally shifted from the first series along the x-axis. The “add=TRUE”
> argument works well. Is there any argument defining the series location within
> plotCI. For example the tool “boxplot” contains the “at” argument. I found
> examples of plotCI where a vertical shift is realized (e.g.  “x=means+1e5”), never a horizontal shift.
>
> Here
> you find data information:
>
> ##data
> value1<-c(1,2,1,2,1,3,4,2,6,1,3,4,2,6)
> value2<-c(1,5,1,2,4,4,4,3,3,1,3,4,9,8)
> typo<-c("C","C","C","C","A","A","A","A","A","B","B","B","B","B")
> tab1<-data.frame(data=cbind(value1,value2,typo))
> colnames(tab1)<-c("value1","value2","typo")
> Â
> tab1$value1<-as.numeric(tab1$value1)
> tab1$value2<-as.numeric(tab1$value2)
>
> ##mean and error first data series
>
> tmp<-
> split(tab1$value1,tab1$typo)
> means<-
> sapply(tmp, mean)
> stdev<-
> sapply(tmp, sd)
> n<-
> sapply(tmp,length)
> ciw<-
> qt(0.975, n) * stdev / sqrt(n)
>
> ##plotting
> first data series
> plotCI(x=means,uiw=stdev,
> col="red", barcol="red", lwd=3, pch=18,
> cex=2,
> xaxt="n", yaxt="n",  ylab='benefit',xlab='typo', yaxs = 'i',ylim=c(0,8))
> axis(side=1,
> at=1:3, labels=names(tmp), cex=0.7)
> Â
>
> ##mean and error second data series
>
> tmp<-
> split(tab1$value2,tab1$typo)
> means<-
> sapply(tmp, mean)
> stdev<-
> sapply(tmp, sd)
> n<-
> sapply(tmp,length)
> ciw<-
> qt(0.975, n) * stdev / sqrt(n)
>
> ##the
> problem: plotting second dataseries
> plotCI(x=means,
> uiw=stdev, col="steelblue", barcol="steelblue", lwd=2,
> pch=18,
> cex=2,
> xaxt="n", yaxt="n", ylab='qualite',xlab='type', yaxs =
> 'i',add=T)Â  ##this series of data is now superposed,
> but would like to put them horizontally shifted next to the red values
>
Hi Rosalinde,
I think you are mixing up the "x" and "y" values. Try this:

library(plotrix)
plotCI(means,uiw=stdev,col="red",lwd=3,pch=18,
  cex=2,xaxt="n",yaxt="n",ylab='benefit',xlab='typo',
  yaxs='i',xlim=c(0.6,3.5),ylim=c(0,8))
axis(side=1,at=1:3,labels=names(tmp),cex=0.7)
plotCI(x=1:3+0.1,y=means,uiw=stdev,col="steelblue",
  lwd=2,pch=18,cex=2,xaxt="n",yaxt="n",ylab='qualite',
  xlab='type',yaxs='i',add=T)

Jim



More information about the R-help mailing list