I have a lattice plot, I am trying to change its background but can't seem to make it work. I am also trying to add Custom heatmap labels (ranging from 1 to 5, and would like to add some text. )
par(bg = "yellow")
levelplot(as.matrix(x = c(1:5),y = c(5:1))
)
Thank you for your time!
Like ggplot
, lattice
is based on grid graphics, not base R graphics, so setting the par
won't have any effect. You can change the lattice pars, or alternatively you can do it after the fact using grid.edit
:
library(lattice)
library(grid)
levelplot(as.matrix(x = c(1:5), y = c(5:1)))
grid.edit("plot_01.background", gp = gpar(fill = "yellow"))
Needless to say, if you are looking for multiple formatting changes, easy annotations, custom scales and rich documentation, you should switch to ggplot:
library(ggplot)
ggplot(data.frame(x = 1:5), aes(x, 0.5, fill = x)) +
geom_tile() +
geom_text(aes(label = x), size = 10) +
coord_equal() +
scale_x_continuous(limits = c(0.5, 5.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 1), expand = c(0, 0)) +
scale_fill_gradient2(low = "#ff77ff", mid = "white", high = "cyan",
midpoint = 3) +
theme_classic(base_size = 18) +
theme(plot.background = element_rect(fill = "#FFFF80"),
plot.margin = margin(150, 50, 150, 50),
legend.background = element_blank())