Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a legend to a plot made with lattice and latticeExtra?

Tags:

r

legend

lattice

I used lattice and lattice extra to plot the observed and predicted values for five different areas. I used xyplot to plot the observed values and then used the as.layer function in lattice extra to add the predicted lines. I would like to add a legend to the graph but havent had any luck.

Here are two example data sets along with the code for graphing.

Example dataset one. I only included two areas.

example1 <- 
structure(list(model_predict = c(10, 25, 95, 23, 56, 70, 56, 
45, 25, 50), Shell_Height = c(27, 33, 115, 25, 46, 50, 35, 35, 
23, 45), SAMS_region_2015 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L), .Label = c("DMV", "LI"), class = "factor")), .Names = c("model_predict", 
"Shell_Height", "SAMS_region_2015"), row.names = c(NA, -10L), class = "data.frame")

Example dataset two. Also only included two areas.

example2 <-
structure(list(Meat_Weight = c(15, 27, 100, 15, 60, 75, 50, 37, 
28, 60), Shell_Height = c(25, 30, 110, 20, 45, 48, 35, 30, 25, 
50), SAMS_region_2015 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L), .Label = c("DMV", "LI"), class = "factor")), .Names = c("Meat_Weight", 
"Shell_Height", "SAMS_region_2015"), row.names = c(NA, -10L), class = "data.frame")

Graphing code

library(lattice)
library(latticeExtra) 

#observed vs predicted values by SAMS region

foo<-xyplot(Meat_Weight~Shell_Height|SAMS_region_2015,data=example2,
        ylab="Meat Weight (g)",xlab="Shell Height (mm)",type="p",
            col="red",pch=3)

#add layer of observed values

foo <- foo + 
       as.layer(xyplot(model_predict~Shell_Height|SAMS_region_2015,
           type = "l", data = example1, col = "blue", lwd = 4, lty = 3))

I would like to add a legend for the plot that has the text Observed and Predicted along with the symbol or line and colors used in the graph for the different variables.

R Info sessionInfo() R version 3.2.1 (2015-06-18) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets 
[7] methods   base     

other attached packages:
 [1] latticeExtra_0.6-26 RColorBrewer_1.1-2  nlme_3.1-120       
 [4] MASS_7.3-40         xlsx_0.5.7          xlsxjars_0.6.1     
 [7] rJava_0.9-6         plyr_1.8.3          RODBC_1.3-12       
[10] lattice_0.20-31    

loaded via a namespace (and not attached):
[1] tools_3.2.1 Rcpp_0.11.6

Any help would be appreciated.

like image 281
user41509 Avatar asked Nov 04 '15 21:11

user41509


2 Answers

xyplot()'s key argument takes a list of parameters that'll handle this quite nicely. The only real trick is that, if you include a lines element in the list, you can include within it an additional type= element directing which items will be plotted as points and which as lines.

## Set up a key
foo_key <- list(x = .97, y = .92, corner = c(1, 1),
            text = list(c("Observed", "Predicted")),
            lines = list(type = c("p", "l"), col = c("red", "blue"),
                         pch = 3, lwd = 4, lty = 3))

## Then pass it in to xyplot() via its 'key' argument
foo <- xyplot(Meat_Weight~Shell_Height|SAMS_region_2015,data=example2,
            ylab="Meat Weight (g)",xlab="Shell Height (mm)",
            type = "p", col = "red", pch = 3,
            key = foo_key)
## ... and carry on with your code, adding a layer and printing the whole thing
foo <- foo +
       as.layer(xyplot(model_predict~Shell_Height|SAMS_region_2015,
           type = "l", data = example1, col = "blue", lwd = 4, lty = 3))

foo

enter image description here

like image 130
Josh O'Brien Avatar answered Nov 15 '22 14:11

Josh O'Brien


You can just add a key= to one of your two xyplot calls

xyplot(Meat_Weight~Shell_Height|SAMS_region_2015,data=example2,
    ylab="Meat Weight (g)",xlab="Shell Height (mm)",type="p",
    col="red",pch=3,
    key=list(columns=2, 
        text=list(lab=c("weight","predict")),
        points=list(pch=c(3,NA), col="red"), 
        lines=list(lty=c(0,3), lwd=4, col="blue"))) + 
as.layer(xyplot(model_predict~Shell_Height|SAMS_region_2015,
    type = "l", data = example1, col = "blue", lwd = 4, lty = 3))

enter image description here

like image 30
MrFlick Avatar answered Nov 15 '22 15:11

MrFlick