[R] Lattice xyplot(): adding a legend
dwinsemius at comcast.net
Thu Oct 20 20:19:09 CEST 2016
> On Oct 20, 2016, at 9:20 AM, 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 source
> 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)
Why are you creating this factor? The `date` column has the desirable properties associated with the "Date" class. Axis labeling will be correct.
> # 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 Precipitation",
#This should have succeeded (.. had you used the dataframe as a data delivery vehicle.)
rainbyday <- xyplot( amount ~ date, data = rain, ...)
> 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), max(rain$amount)))),
> pch = 20,
> col = c("black","red","dark green","dark blue","dark goldenrod","dark magenta"),
Unless you map those colors to the 'station' vector (as I demonstrated in a earlier reply, the coloring of the points will be arbitrary.
> 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"),
Unless you map those colors to the levels of 'station' as I earlier demonstrated, the coloring in the legend will be arbitrary.
> pch = 20, cex = 1.2)))
> # Plot it
> # Reset display parameters
> When I eliminate the "$" operator this is the error message:
We don't have any way of viewing that code. I feel confident it has an error:
Here is code that I believe gives the correct labeling and coloring of points and legend. It took some experimentation to get the points in the legend to fill correctly:
rainbyday2 <- xyplot(amount ~ date, data = rain, main = "Area Precipitation",
ylab = "Daily Total Amount (in)", xlab = "Date",
scales = list(x=list(at=rain$date[ c(1,8,15,22,29,36,43,50,57,62)], rot = 90),
y = list(at=c(min(rain$amount), max(rain$amount)))),
pch = 20,
col = c("black","red","dark green","dark blue","dark goldenrod","purple")[rain$station],
# Needed to use the 'station'-factor to properly index the colors. Otherwise there is no 1-1 relationship.
key = list( text=list(levels(rain$station)),
fill = c("black","red","darkgreen","darkblue","darkgoldenrod","purple"),
pch = 16,
x = 0.2, y = 0.6, corner = c(0, 0), points = TRUE)); print(rainbyday2)
Reading the archives shows that this is not the first time this problem of getting fills with the `key` parameter for xyplot has caused problems for you. Back in 2013 Richard Heiberger and S Ellison were your correspondents. I found that using `simpleKey` was unsuccessful due to problematic defaults.
> 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/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Alameda, CA, USA
More information about the R-help