[R] Lattice xyplot(): adding a legend

Rich Shepard rshepard at appl-ecosys.com
Thu Oct 20 18:20:17 CEST 2016


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.

David,

   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)

# 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",
                     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"),
                     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
plot(rainbyday)

# Reset display parameters
par(opar)
-----

   When I eliminate the "$" operator this is the error message:
> source("plot-rain-by-day.R")
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?

Rich


-------------- next part --------------
structure(list(station = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("0.3E", 
"0.6W", "1..0WNW", "1.5N", "4.3WNW", "Airport"), class = "factor"), 
    date = structure(c(32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 
    40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
    52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L
    ), .Label = c("2013-12-01", "2013-12-02", "2013-12-03", "2013-12-04", 
    "2013-12-05", "2013-12-06", "2013-12-07", "2013-12-08", "2013-12-09", 
    "2013-12-10", "2013-12-11", "2013-12-12", "2013-12-13", "2013-12-14", 
    "2013-12-15", "2013-12-16", "2013-12-17", "2013-12-18", "2013-12-19", 
    "2013-12-20", "2013-12-21", "2013-12-22", "2013-12-23", "2013-12-24", 
    "2013-12-25", "2013-12-26", "2013-12-27", "2013-12-28", "2013-12-29", 
    "2013-12-30", "2013-12-31", "2014-01-01", "2014-01-02", "2014-01-03", 
    "2014-01-04", "2014-01-05", "2014-01-06", "2014-01-07", "2014-01-08", 
    "2014-01-09", "2014-01-10", "2014-01-11", "2014-01-12", "2014-01-13", 
    "2014-01-14", "2014-01-15", "2014-01-16", "2014-01-17", "2014-01-18", 
    "2014-01-19", "2014-01-20", "2014-01-21", "2014-01-22", "2014-01-23", 
    "2014-01-24", "2014-01-25", "2014-01-26", "2014-01-27", "2014-01-28", 
    "2014-01-29", "2014-01-30", "2014-01-31"), class = "factor"), 
    amount = c(NA, NA, 0.01, 0, 0, 0, 0.1, 0.22, 0.49, 0.12, 
    0.47, 0.7, 0.24, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0, 0, 
    0.01, NA, 0, 0, 0, NA, 0.07, 0.48, 0.03, NA, 0.32, 0.92, 
    0.04, NA, 0, 0.01, 0.01, 0, 0, NA, 0, 0, 0.08, 0.01, 0.02, 
    0.01, 0.01, NA, 0.09, 0.02, 0.13, 0.01, NA, 0.02, 0.01, 0.01, 
    NA, NA, NA, 0, 0.04, 0.02, 0.01, 0.02, 0.01, NA, 0, 0.11, 
    0.24, 0.54, 0.15, 0.48, 0.02, 0.01, NA, 0.01, NA, NA, 0, 
    0, 0, 0, 0, 0, 0.06, 0.48, 0.03, 0.03, NA, NA, NA, NA, 0.05, 
    0, 0, 0.01, 0, 0, 0, 0, 0, NA, 0.07, 0, 0.01, 0.02, 0.01, 
    0.01, 0.1, 0.01, 0.01, 0, 0, 0.04, 0.01, NA, 0, 0.01, 0.01, 
    0, 0.05, NA, NA, 0.01, 0.01, 0.02, 0, 0, 0, 0.11, 0.22, 0.54, 
    0.54, 0.44, 0.71, 0.21, 0.01, 0.02, 0.01, 0.01, 0.01, 0.01, 
    0, 0, 0, 0, 0, 0, 0, 0, 0.06, 0.46, 0.02, 0.02, 0.87, 0.1, 
    0, 0, 0, NA, 0, 0, 0, 0, 0, 0.07, 0.01, 0.01, 0.02, 0, 0, 
    0.07, 0, 0.1, NA, NA, 0.02, NA, 0, 0, 0, 0, 0, NA, 0.05, 
    0, 0.01, 0.01, 0, 0, 0.05, 0.27, 0.42, 0.06, 0.01, 0.9, 0.36, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.06, 0.36, 0.12, 
    0.01, 0.05, 0.25, 0.91, 0.03, 0, 0, NA, 0, 0, 0, 0, 0, 0, 
    0.07, NA, 0.01, 0.01, 0.01, NA, 0.09, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 0.01, 0.01, 0.01, 0, 0, 0, 0.11, 
    0.23, 0.49, 0.1, 0.45, 0.6, 0.28, 0, 0.01, NA, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0.06, 0.48, 0.02, 0.02, 0.4, 1.06, 0.13, 
    0, 0, 0.03, 0.01, 0, 0, 0, 0, 0, 0.12, 0.02, NA, NA, NA, 
    NA, NA, 0.03, 0.24, 0.02, 0.02, 0.05, 0.01, 0, NA, 0, 0.02, 
    0, 0.01, 0.04, 0.02, 0.04, 0.01, NA, 0, 0.13, 0.34, 0.84, 
    0.61, 0.61, 1, 0.44, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 
    0.02, 0.02, 0.02, 0.01, NA, 0, 0, 0, 0.07, 0.62, 0.1, 0.05
    )), .Names = c("station", "date", "amount"), class = "data.frame", row.names = c(NA, 
-341L))


More information about the R-help mailing list