[R] bargraph.CI and arrows
Reid, Doug (MNRF)
doug.reid at ontario.ca
Wed Feb 4 15:50:07 CET 2015
Hello R-Help folks. I'm fairly new to R and particularly the graphing functions. The figures I have made look great though and so I want to use it more.
I am using bargraph.CI and have made several charts successfully using raw data. I have a figure, however that doesn't lend itself to the functions (calculations) in bargraph.CI because it does not present conventional means and standard errors. Still I would like the figure to have the same look as others I am presenting so I want to use the same plot function
Selection ratios and associated standard errors were calculated as per Manley et al (2002) to assess cover type preference for some critters. Selection Ratios were calculated at two scales from a sample of individual animal locations from the population (used points), and an equal number of random locations (available points): 2nd order across the entire area of possible occurrence, and 3rd order within home ranges.
I want to make a grouped bar chart, using these results:
Cover
SR
Order
SE
Water
0.3309
2nd
0.027909
Open Low
0.22865
2nd
0.024847
Treed Low
1.083006
2nd
0.01808
Sparse
1.966667
2nd
0.254225
Deciduous
0.792079
2nd
0.087706
Mixedwood
0.972441
2nd
0.060019
Conifer
2.226475
2nd
0.048635
Disturbance
0.410596
2nd
0.025288
Other
0.125
2nd
0.124985
Water
0.221983
3rd
0.02152
Open Low
0.648352
3rd
0.08363
Treed Low
1.138462
3rd
0.021742
Sparse
0.976744
3rd
0.149728
Deciduous
0.928571
3rd
0.114005
Mixedwood
1.032432
3rd
0.072452
Conifer
1.880137
3rd
0.046046
Disturbance
0.376321
3rd
0.027415
Other
0
3rd
0
Selection Ratio (SR) is calculated using
Proportion of used points (oi)/proportion of available points (ði)
Standard error for the selection ratio (SE)
s.e = (sqrt((ði*(1-ði)/ n*(oi^2)) where n is the total number of points
These equations do not lend themselves to making the calculations in R, so I am simply trying to plot the results. Here is my code:
################################################
data <- read.csv("data.csv")
#Specify the order that factors appear in (alphabetical is default)
data$Cover <- ordered (data$Cover, levels = c("Disturbance", "Deciduous", "Mixedwood",
"Conifer", "Sparse", "Treed Low", "Open Low", "Water", "Other"))
#define errors
sem = function(SE) (SE*1)
errors = with(data, tapply(SE, Cover, sem))
errors
#define benchmark value
benchmark <- 1.0
library(sciplot)
library(graphics)
#make default barchart to determine x-coordinates for error bars#
bargraph.CI(Cover, SR, Order, data = data, lc=FALSE, uc=FALSE) ->bpN.out
bpN.out
#make barchart so it looks right#
par(mar = c(10,8,1,1), oma = c(1,1,1,1))
bargraph.CI(Cover, SR, Order, data = data, ylim=c(0,2.5), ylab = "Selection Ratio",
cex.names = 1, cex.axis = 1.5, cex.lab = 1.5, las=2, err.width = 0.03, legend=F,
axis.lty=1, cex=1.1,lc=FALSE, uc=FALSE)
#Plot benchmark line at SR = 1.0
abline(h=benchmark, lty = "dotdash", lwd=2)
#Plot error bars
for(i in 1:length(bpN.out)){
arrows(bpN.out$xvals[i],means[i],bpN.out$xvals[i],means[i]+errors[i], angle=90, length=.1)
arrows(bpN.out$xvals[i],means[i],bpN.out$xvals[i],means[i]-errors[i], angle=90, length=.1)
}
###
Two questions:
1. Though I have specified lc=FALSE and uc=FALSE when I call for the plot I get a warning for each of the 18 bars produced, I believe because it cannot calculate a confidence interval. How can I suppress the function within bargrapg.CI that wants to make the calculation? When I call bpN.out I also get the full list of N/A values for the $CI. Wondering if I can prevent this from happening, or if I need to.
2. Instead of plotting error bars I get, "Error in means[i] + errors[i] : non-numeric argument to binary operator". Is there something else I should be doing to get the SE values I have calculated to define the width of the error bars I want to plot?
Thanks for your help!
Douglas E. B. Reid PhD
Boreal Silviculture Reasearch Program Lead
Centre for Northern Forest Ecosystem Research
(807) 343-4008
[[alternative HTML version deleted]]
More information about the R-help
mailing list