Home > Back-end >  R lattice levelplot, changing the background color and custom colorscale labels
R lattice levelplot, changing the background color and custom colorscale labels

Time:04-27

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))
)

enter image description here

Thank you for your time!

CodePudding user response:

library(lattice)
trellis.par.set(background = list(col="yellow"))
levelplot(as.matrix(x = c(1:5),y = c(5:1)), 
          scales=list(x=list(at=1:5, labels=LETTERS[1:5])))

Created on 2022-04-26 by the enter image description here

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())

enter image description here

  • Related