Search code examples
latticenlme

How to change the legend text color in lattice so that it works nicely with thematic?


I want to make plots and analyze data in a dark themed IDE. I thought maybe I could accomplish this with the thematic package, and it mostly works, but I am struggling with lattice plots. At the very least, the lattice plots made by the nlme package.

Here is a minimal example. Note the legend text is utterly unreadable in the screenshot below.

library(nlme)
library(thematic)

# Some pretty color theme
thematic_on(
  bg = "#0f0b15",
  fg = "#b8c6d5",
  accent = "#bf8f8f",
  qualitative = RColorBrewer::brewer.pal(8, "Dark2")
)

plot(Machines)

legend text color is black on off-black

How can I modify the code so that thematic does what it claims to do and I can still read the lattice legend?

Note that it seems like the lattice::settings look good otherwise.

lattice::settings graphical output


Solution

  • This issue seems to be caused by draw.key. As a workaround, I replace the default key with a new key using simpleKey and the data stored in the grid object. First save the plot into the object, e.g. p. Then, you can extract the key with:

    k = p$legend$top$args$key
    

    As far as I can tell, in many cases you can get away with the following code:

    update(p,
      key = simpleKey(text = k$text$levels, col = "#b8c6d5")
    )