Plot mean and standard deviation by category

mean, plot, r

Solution

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 `NA`s 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)

Problem

I'm trying to plot a plot with mean and sd bars by three levels of a factor. (After two hours of searching on the internet, then checking the Rbook and Rgraphs book I'm still not finding the answer. I think this is because it is a very simple question.) I have a simple data frame with three columns: my categories, mean, sd. I would like to do a plot with the mean by category and its sd bars, just like this one (edit: link broken) My dataframe looks like this ``` color mean.temp sd black 37.93431 2.267125 red 37.01423 1.852052 orange 36.61345 1.339032 ``` I'm so sorry for asking this dumb question but I sincerely couldn't find any simple answer to my simple question.

Original source