Plot mean and standard deviation by category

Create a data.frame holding your data:

foo <- data.frame(color=c("black","red","orange"),
    mean.temp=c(37.93431,37.01423,36.61345),
    sd=c(2.267125,1.852052,1.339032))

Now, we first plot the means as dots, making sure that we have enough room horizontally (xlim) and vertically (ylim), suppressing x axis annotation (xaxt="n") and all axis labeling (xlab="", ylab="").

plot(1:3,foo$mean.temp,pch=19,xlab="",ylab="",xaxt="n",xlim=c(0.5,3.5),
    ylim=c(min(foo$mean.temp-foo$sd),max((foo$mean.temp+foo$sd))))

Next, we plot the standard deviations as lines. You could also use three separate lines commands, which may be easier to read. This way, we first collect the data into matrices via rbind(). R will automatically turn these matrices into vectors and recycle them. The NAs are there so we don't join the end of one line to the beginning of the next one. (Try removing the NAs to see what happens.)

lines(rbind(1:3,1:3,NA),rbind(foo$mean.temp-foo$sd,foo$mean.temp+foo$sd,NA))

Finally, annote the x axis:

axis(side=1,at=1:3,labels=foo$color)

means with sds


With ggplot:

read data:

df=read.table(text=' color mean.temp   sd
1 black 37.93431 2.267125

2 red 37.01423 1.852052

3 orange 36.61345 1.339032',header=TRUE)

plotting:

 ggplot(df, aes(x=color, y=mean.temp)) + 
     geom_errorbar(aes(ymin=mean.temp-sd, ymax=mean.temp+sd), width=.2) +
     geom_line() +
    geom_point()

output

enter image description here

Tags:

Plot

R

Mean