R-grid: add legend to the figure

I am using a lattice package and I want to add a legend to my figure. The auto.key documentation and legend are very confusing and cannot determine the correct syntax for adding a legend. Here is my code:

xyplot(y ~ x, df, pch=19, col=rgb(0.2, 0.4, 0.8, 0.7), cex=2, scales=list(cex=1.7), xlab=list("x", cex=1.ales=list(cex=1.7), xlab=list("x", cex=1.7), ylab=list("y", cex=1.7), main=list("Linear Regression w. Polynomial Attributes", cex=1.6), auto.key=T, panel = function(x, y, ...) { panel.xyplot(x, y, ...) llines(x, predict(lm.xtend), col="purple", lwd=6, lty=3) llines(x, predict(ridge.lin), col="darkgreen", lwd=6, lty=2) }) 

The graph is shown below, so I just want to add a legend for the rows. enter image description here

+6
source share
1 answer

I don’t know exactly how you want it to look, but here is the beginning. Instead of auto.key=T put:

 key=list(space="right", lines=list(col=c("purple","darkgreen"), lty=c(3,2), lwd=6), text=list(c("Purple Line"," Dark-green Line")) ) 

This will put the key on the right side of the chart. Instead, you can use top, bottom, or left. If you want it to be inside the plot, get rid of space and use corner=c(0,1) instead. The first number is the location on the x axis (0 to 1), the second for the y axis. Thus, it will lie in the upper left corner.

+9
source

Source: https://habr.com/ru/post/973293/


All Articles