问题
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.
回答1:
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)
回答2:
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
来源:https://stackoverflow.com/questions/24626280/plot-mean-and-standard-deviation-by-category