Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting standard errors in R

Tags:

r

I have data on the catch rate of certain species of fish.

fish 1  fish 2  fish 3
0.000   3.265   9.872
2.147   1.013   0.000

I have calculated the mean catch rate for each fish using:

a <- colMeans(df)

I have also calculated the standard error:

stdError <- (sapply(df,sd))/sqrt(length(df))

I have created a dotplot using:

dotplot(a, xlab="mean catch", ylab = "species",las =2,)

How do I add error bars to this plot? I would prefer not to use ggplot if possible. I am currently using the inbuilt functions in R but have access to Lattice.

Sorry for what is probably a basic question, I am entirely new to plots in R.

like image 634
user3489562 Avatar asked Aug 11 '14 15:08

user3489562


1 Answers

dotplot is a lattice function and most default lattice functions don't have great support for confidence intervals. The Hmisc package extends most lattice functions to better incorporate confidence intervals.

Here's an example of how you would use it. Note that we combine the data you want to plot here into a data.frame so we can use a proper formula symtax

mm<-data.frame(a,stdError, fish=names(a))

library(lattice)
library(Hmisc)
Dotplot(fish~Cbind(a, a-stdError, a+stdError), mm, 
    xlab="mean catch", ylab = "species",las =2)

and this produces

enter image description here

Note that the Hmisc version of the function is called Dotplot while the lattice version is called dotplot; capitalization matters.

Here I've just added/subtracted one standard error from the mean. You can calculate the confidence intervals however you like.

like image 81
MrFlick Avatar answered Sep 30 '22 17:09

MrFlick