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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With