[R] Lattice xyplot(): adding a legend

Rich Shepard rshepard at appl-ecosys.com
Wed Oct 19 19:48:41 CEST 2016


   After reading ?xyplot and pages 161-162 in Deepayan's book I'm still not
getting the syntax correct to add a legend to a scatterplot. The data are
attached as filename rain.dput.

   Without the legend (and testing interactively) the data plot but not the
legend. Sourcing the script opens the display but neither data nor legend
appear. I know that I'll need to adjust the legend's position so it fits in
the empty space at the top, left center, but I need to see where it is
first.

   I tried adding the legend to the xyplot() command with no difference in
the results.

   In both cases R displays this error:

source("plot-rain-by-day.R")
Error in as.graphicsAnnot(legend) (from plot-rain-by-day.R#16) :
   argument "legend" is missing, with no default

   I've not found an example to follow so pointers to how to add the legend
are needed.

   The script:

# This scatter plot has rain amounts by day for each station, distinguished by color.

# load rain data file
rain

# 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, 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)), pch=as.numeric(rain$station), col=c("black","red","dark green","dark blue","dark goldenrod","dark goldenrod"))

# Add a legend
legend(x=0.83, y=-1.65, pch=as.numeric(rainbyday), col=c("black","red","dark green","dark blue","dark goldenrod","dark goldenrod"))

# Plot it
plot(rainbyday)

# Reset display parameters
par(opar)

TIA,

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,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 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(16071, 16072, 16073, 16074, 16075, 16076, 
    16077, 16078, 16079, 16080, 16081, 16082, 16083, 16084, 16085, 
    16086, 16087, 16088, 16089, 16090, 16091, 16092, 16093, 16094, 
    16095, 16096, 16097, 16098, 16099, 16100, 16101, 16040, 16041, 
    16042, 16043, 16044, 16045, 16046, 16047, 16048, 16049, 16050, 
    16051, 16052, 16053, 16054, 16055, 16056, 16057, 16058, 16059, 
    16060, 16061, 16062, 16063, 16064, 16065, 16066, 16067, 16068, 
    16069, 16070, 16071, 16072, 16073, 16074, 16075, 16076, 16077, 
    16078, 16079, 16080, 16081, 16082, 16083, 16084, 16085, 16086, 
    16087, 16088, 16089, 16090, 16091, 16092, 16093, 16094, 16095, 
    16096, 16097, 16098, 16099, 16100, 16101, 16040, 16041, 16042, 
    16043, 16044, 16045, 16046, 16047, 16048, 16049, 16050, 16051, 
    16052, 16053, 16054, 16055, 16056, 16057, 16058, 16059, 16060, 
    16061, 16062, 16063, 16064, 16065, 16066, 16067, 16068, 16069, 
    16070, 16071, 16072, 16073, 16074, 16075, 16076, 16077, 16078, 
    16079, 16080, 16081, 16082, 16083, 16084, 16085, 16086, 16087, 
    16088, 16089, 16090, 16091, 16092, 16093, 16094, 16095, 16096, 
    16097, 16098, 16099, 16100, 16101, 16040, 16041, 16042, 16043, 
    16044, 16045, 16046, 16047, 16048, 16049, 16050, 16051, 16052, 
    16053, 16054, 16055, 16056, 16057, 16058, 16059, 16060, 16061, 
    16062, 16063, 16064, 16065, 16066, 16067, 16068, 16069, 16070, 
    16071, 16072, 16073, 16074, 16075, 16076, 16077, 16078, 16079, 
    16080, 16081, 16082, 16083, 16084, 16085, 16086, 16087, 16088, 
    16089, 16090, 16091, 16092, 16093, 16094, 16095, 16096, 16097, 
    16098, 16099, 16100, 16101, 16040, 16041, 16042, 16043, 16044, 
    16045, 16046, 16047, 16048, 16049, 16050, 16051, 16052, 16053, 
    16054, 16055, 16056, 16057, 16058, 16059, 16060, 16061, 16062, 
    16063, 16064, 16065, 16066, 16067, 16068, 16069, 16070, 16071, 
    16072, 16073, 16074, 16075, 16076, 16077, 16078, 16079, 16080, 
    16081, 16082, 16083, 16084, 16085, 16086, 16087, 16088, 16089, 
    16090, 16091, 16092, 16093, 16094, 16095, 16096, 16097, 16098, 
    16099, 16100, 16101, 16040, 16041, 16042, 16043, 16044, 16045, 
    16046, 16047, 16048, 16049, 16050, 16051, 16052, 16053, 16054, 
    16055, 16056, 16057, 16058, 16059, 16060, 16061, 16062, 16063, 
    16064, 16065, 16066, 16067, 16068, 16069, 16070, 16071, 16072, 
    16073, 16074, 16075, 16076, 16077, 16078, 16079, 16080, 16081, 
    16082, 16083, 16084, 16085, 16086, 16087, 16088, 16089, 16090, 
    16091, 16092, 16093, 16094, 16095, 16096, 16097, 16098, 16099, 
    16100, 16101), class = "Date"), 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"), row.names = c(NA, -341L), class = "data.frame")


More information about the R-help mailing list