[R] help with plotting error bars on xyplot!
Paulo Pagliari
pagli005 at umn.edu
Tue Mar 3 19:07:44 CET 2015
Dear R helpers,
I am trying to create a xyplot similar to this one:
https://stat.ethz.ch/pipermail/r-help/2008-June/164968.html
I can plot the data correctly when I only have one Y. However, I need to add another Y (Y2) to the plot and each Y must have their own error bars and also different symbols. You will see that in this plot symbol changes based on X and not Y. The symbols should change by Y category, for example in the example below, the symbols with the errors bars sould be one symbol while the symbols without errors bars should have a different symbol and also should have error bars. Any help will be much appreciated.
Hr = c(0,1,2,3,4,5,0,1,2,3,4,5)
DRUG = rep(c("D", "P"), each=6)
Y = c(1,2,2,2,2,1,3, 4, 4,4, 4, 3)
Y2 = c(2,3,3,3,3,2,4, 5, 5,5, 5, 4)
data = data.frame(Hr, DRUG, Y, Y2)
data$lower1 = data$Y - .5
data$upper1 = data$Y + .5
data$lower2 = data$Y2 - .5
data$upper2 = data$Y2 + .5
prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
x <- as.numeric(x)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
list(ylim = range(y, uy, ly, finite = TRUE)) }
panel.ci <- function(x, y, ly, uy, subscripts, pch = c(1, 16), ...) {
x <- as.numeric(x)
y <- as.numeric(y)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
panel.arrows(x, ly, x, uy, col = "black",
length = 0.25, unit = "native",
angle = 90, code = 3)
panel.xyplot(x, y, pch = c(1, 16), ...)}
xyplot(Y+Y2 ~ Hr,
data=data,
ly = data$lower1,
uy = data$upper1,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
auto.key = list(space = "top", text = c( "D","P"), points = FALSE,
lines = TRUE, columns=2))
Paulo H. Pagliari, PhD.
Assistant Professor Extension Soil Scientist
University of Minnesota
Southwest Research and Outreach Center
23669 130th St.
Lamberton, MN 56152
Tel. 507-752-5065
FAX 507-752-5097
email: pagli005 at umn.edu
[[alternative HTML version deleted]]
More information about the R-help
mailing list