Reputation: 5907
I am trying to make "Level Plots" in R, and I am following the instructions from here: https://www.rdocumentation.org/packages/lattice/versions/0.10-10/topics/levelplot
library(lattice)
x <- seq(pi/4, 5 * pi, length = 100)
y <- seq(pi/4, 5 * pi, length = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
ylab="", main="Weird Function", sub="with log scales",
colorkey = FALSE, region = TRUE)
Is there any way to add a legend to this plot? There does not seem to be any indication of a legend option in the "rdocumentation".
I found a similar post over here that shows how to add a legend: R - How to add legend title to levelplot saved to a variable?
But this seems to be a complicated way to add a legend.
Is there really no simple way to add a legend to this level plot?
Upvotes: 0
Views: 436
Reputation: 11995
You simply need to assign colorkey = TRUE
:
levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
ylab="", main="Weird Function", sub="with log scales",
colorkey = TRUE, region = TRUE)
Upvotes: 1