[R] show 0 at y axis in xyplot lattice
Duncan Mackay
dulcalma at bigpond.com
Tue Sep 12 03:11:28 CEST 2017
Hi Maria
Rule 1 make sure your data is in the right format
dat <- source("G:/1/savedat.txt")
> dat
$value
Location Date Zn2 upper.zn lower.zn
1 upstream 2016-04-27 29.92477 55.59800 13.912207
2 spill 2016-04-27 12.84040 22.07006 6.964934
3 downstream 2016-04-27 22.49673 41.60901 11.739109
4 upstream 2016-06-28 23.98425 45.60219 10.690640
5 spill 2016-06-28 41.51336 77.99893 20.142426
6 downstream 2016-06-28 15.51232 26.94059 7.044781
7 upstream 2016-08-25 21.73037 40.95852 10.354620
8 spill 2016-08-25 13.42082 22.96901 7.472893
9 downstream 2016-08-25 10.99209 20.17471 5.324318
10 upstream 2016-10-25 20.82462 40.60564 9.602007
11 spill 2016-10-25 14.01283 25.07032 7.830504
12 downstream 2016-10-25 15.67740 30.42278 6.971944
13 upstream 2016-12-21 28.14966 51.79390 14.384139
14 spill 2016-12-21 13.91587 23.94368 8.164688
15 downstream 2016-12-21 13.02749 24.46930 5.826650
16 upstream 2017-02-20 31.16736 55.51858 15.938211
17 spill 2017-02-20 12.47368 22.03830 6.725540
18 downstream 2017-02-20 17.65741 33.23577 8.519928
$visible
[1] TRUE
dat <- dat$value
dat$dDate <- as.Date(as.character(dat$Date))
str(dat)
'data.frame': 18 obs. of 6 variables:
$ Location: Factor w/ 3 levels "downstream","spill",..: 3 2 1 3 2 1 3 2 1 3 ...
$ Date : Factor w/ 6 levels "2016-04-27","2016-06-28",..: 1 1 1 2 2 2 3 3 3 4 ...
$ Zn2 : num 29.9 12.8 22.5 24 41.5 ...
$ upper.zn: num 55.6 22.1 41.6 45.6 78 ...
$ lower.zn: num 13.91 6.96 11.74 10.69 20.14 ...
$ dDate : Date, format: "2016-04-27" "2016-04-27" "2016-04-27" "2016-06-28" ...
You code to reproduce the xyplot is not reproducible as it contains user defined objects which you have not included.
Here is something to get you started,
If you want the measurement dates on the x-axis you will need to put the required dates and format in the scales argument.
Using par.settings for the symbols an lines will pass those values onto key
xyplot(upper.zn + Zn2 + lower.zn ~ dDate | Location, data=dat,
type = "b",
as.table = TRUE,
par.settings = list(strip.background = list(col = "transparent"),
superpose.symbol = list( col=c("red", "black", "red"),
cex = 1,
pch = c(22, 21, 22)),
superpose.line = list (col=c("red", "black", "red"),
lty = c(2, 1, 2),
lwd = 1)
),
auto.key = list(points = TRUE, lines = TRUE),
scales = list(x = list(alternating = FALSE,
at = seq(from = as.Date("2016-04-01"), to = as.Date("2017-04-01"), by = "quarter"),
label = format(seq(from = as.Date("2016-04-01"), to = as.Date("2017-04-01"), by = "quarter"), "%Y %b"),
relation = "same",
rot = 60),
y = list(alternating = FALSE,
at = seq(0,80,20),
relation = "same",
rot = 0)
),
# pch=c(22, 21, 22),
# lty=c(2, 1, 2),
xlim = range(seq(from = as.Date("2016-04-01"), to = as.Date("2017-04-01"), by = "quarter")),
ylim=c(-5, 80),
index.cond=list(c(3, 1, 2)),
ylab="Percent (%)",
# par.strip.text=list(col="white", font=2, lines=1.5),
# lattice.options = modifyList(lattice.options(), list(skip.boundary.labels = 0)),
# par.settings=my.settings, col=c("red", "black", "red"), fill=c("red", "black", "red"),
# key=dat_key,
# scales = list(x = list(at = sdate, labels = format(sdate, "%b-%y"))), xlab="Date",
panel = function(x, y, ...) {
panel.grid(h = -1, v = 0, lwd=1, lty=3, col="grey")
panel.abline(v=dat$dDate, lwd=1, lty=3, col="grey")
panel.xyplot(x, y, ...)
}
)
As you have upper and lower levels of Zn you may want to look at ?panel.polygon
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2350
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Maria Lathouri via R-help
Sent: Tuesday, 12 September 2017 05:33
To: R-help Mailing List
Subject: [R] show 0 at y axis in xyplot lattice
Dear all
I am trying to make a plot in xyplot lattice by groups. I would like to show "0" in y axis but I don't want to be aligned with the x axis. I want to be a little bit above.
I have tried many options but I don't get what I want. I also tried ylim=c(0, 80) but both 0 and 80 are fully aligned with the x-axis and the upper boundary of the plot, respectively:
xyplot(upper.zn + Zn2 + lower.zn ~ sdate | Location, type="b", as.table=TRUE, data=dat, pch=c(22, 21, 22), lty=c(2, 1, 2), ylim=c(0, 80),
index.cond=list(c(3, 1, 2)), ylab="Percent (%)", par.strip.text=list(col="white", font=2, lines=1.5),
lattice.options = modifyList(lattice.options(), list(skip.boundary.labels = 0)),
par.settings=my.settings, col=c("red", "black", "red"), fill=c("red", "black", "red"),
key=dat_key, scales = list(x = list(at = sdate, labels = format(sdate, "%b-%y"))), xlab="Date",
panel = function(x, y, ...) {
panel.grid(h = -1, v = 0, lwd=1, lty=3, col="grey")
panel.abline(v=sdate, lwd=1, lty=3, col="grey")
panel.xyplot(x, y, ...)
}
)
when I use the following, 80 is a bit lower than the upper boundary of the plot, which is what I want, but 0 is not showing:
xyplot(upper.zn + Zn2 + lower.zn ~ sdate | Location, type="b", as.table=TRUE, data=dat, pch=c(22, 21, 22), lty=c(2, 1, 2),
index.cond=list(c(3, 1, 2)), ylab="Percent (%)", par.strip.text=list(col="white", font=2, lines=1.5),
lattice.options = modifyList(lattice.options(), list(skip.boundary.labels = 0)),
par.settings=my.settings, col=c("red", "black", "red"), fill=c("red", "black", "red"),
key=dat_key, scales = list(x = list(at = sdate, labels = format(sdate, "%b-%y")), y=list(at=c(0, 20, 40, 60, 80))), xlab="Date",
panel = function(x, y, ...) {
panel.grid(h = -1, v = 0, lwd=1, lty=3, col="grey")
panel.abline(v=sdate, lwd=1, lty=3, col="grey")
panel.xyplot(x, y, ...)
}
)I have also attached a reproducible example in case you want to see in more detail my data.
I would very much appreciate any suggestions on this.
Thank you in advance.
Kind regards,Maria
More information about the R-help
mailing list