Adding legends to level plots

I am trying to make "Level Plots" in R, and I am following the instructions from here: levelplot function - RDocumentation

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".

Is there really no simple way to add a legend to this level plot?

Thanks

you set colorkey = FALSE ... try setting it as TRUE ?

1 Like

this seems to have been the problem! thank you so much for your help!