[R] Lattice xyplot(): adding a legend
bgunter.4567 at gmail.com
Thu Oct 20 19:02:42 CEST 2016
1. Your par(), opar() business is junk -- lattice displays do not use or
modify these. See ?trellis.par.set .
2. You do *not* need rain$ in your formula with the data=rain argument; you
*do* need it in the at argument and elsewhere. The data argument only
controls where the variables in the formula (or subset or group) are
evaluated. This is clearly stated in ?xyplot. Did you read it?
If that doesn't work, your data are screwed up. Incidentally, I counted 3
times where rain$ appears, not 4. Did I miss one or did you err?
3. *Please* reread the key documentation in xyplot carefully. The key
parameters are determined by the points, etc component lists of the key.
The par.settings list controls the **plot** settings.
Finally, are you sure you want date as a factor? Wouldn't that get the date
order all messed up?
Please note that all of this is clearly documented. It's terse, but it's
there. You need to pay careful attention, and then you,David, and everyone
else will be happy and free of frustration.
On Oct 20, 2016 6:21 PM, "Rich Shepard" <rshepard at appl-ecosys.com> wrote:
> On Wed, 19 Oct 2016, David Winsemius wrote:
> I am getting annoyed, exhausted, and frustrated reading code like that.
>> Never, ever, ... ever, use the "$" operator in a formula. Use the 'data'
>> argument and the 'formula' as they are supposed to be used.
> I apologize for annoying, exhausting, and frustrating you. It is not my
> intent and I learned to use the data argument and not the "$" operator a
> while ago. I have not be able to find why that's not working here.
> Following the 'usage' section in ?xyplot I specify 'x'/'formula' followed
> by the data specification. When I omit the "$" operator R (version 3.3.1)
> displays an empty device window and an error message (shown below the
> file). The dput() file is attached and the script presented here:
> # This scatter plot displays amount by day for each station, distinguished
> # by color. 'plot-rain-by-date.R'
> # load rain data file
> rain <- read.table("daily_records.dat", header = TRUE, sep = ",", quote =
> rain$date <- as.Date(rain$date)
> # Create a factor from the date
> raindate <- as.factor(rain$date)
> # Save original plotting parameters
> opar <- par(xpd=NA,no.readonly=T)
> # Prepare the plot of the data
> rainbyday <- xyplot(rain$amount ~ raindate, data = rain, main = "Area
> ylab = "Daily Total Amount (in)", xlab = "Date",
> scales = list(x=list(at=c(1,8,15,22,29,36,43,50,57,62),
> rot = 90),
> y = list(at=c(min(rain$amount),
> pch = 20,
> col = c("black","red","dark green","dark blue","dark
> goldenrod","dark magenta"),
> key = simpleKey(text = levels(rain$station)[1:6],
> x = 0.2, y = 0.6, corner = c(0, 0), points = TRUE),
> par.settings = list(plot.symbol =
> list(c("black","red","dark green","dark blue",
> "dark goldenrod","dark
> magenta"), pch = 20, cex = 1.2)))
> # Plot it
> # Reset display parameters
> When I eliminate the "$" operator this is the error message:
> Error in xyplot.formula(amount ~ raindate, data = rain, main =
> "Area Precipitation", (from plot-rain-by-day.R#14) :
> object 'amount' not found
> The "$" operator appears 4 times; removing any one (or all) of them
> produces the same not found error. If there's another syntax error in the
> xyplot() call, or a logic error in the script please show me my mistake.
> With the "$" operator there's no error, but the key does not use pch=20;
> have I mis-placed Duncan's suggested par.settings?
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help