17

The default theme on my installation is something which maps the values to pink and cyan. How to change it for example to a gray scale theme?

user442446
  • 1,099
  • 3
  • 12
  • 13

5 Answers5

16

You can use

library(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))

which turns on black-and-white suitable for printing. I've also played with things like

sb <- trellis.par.get("strip.background") 
sb[["col"]][1] <- "lightgray"
trellis.par.set("strip.background", sb) 

which changes just the header background.

And I thought people only asked ggplot2 questions here :) Nice to see some lattice for a change.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    indeed, with enough effort one may convince lattice to produce plots that have [little to envy to ggplot2's polished looks](http://latticeextra.r-forge.r-project.org/#layer&theme=ggplot2like). There's no latticelike theme for ggplot2 that I know of. – baptiste Jul 28 '12 at 02:58
  • 1
    This did not work, at least not within org-mode. Had to use `trellis.par.set(canonical.theme(color = FALSE))` – Henrik Sep 26 '12 at 12:23
  • @Henrik Same for me - thanks for sharing your solution! – deca Feb 04 '18 at 19:11
13

Thanks for the answers guys! It also helped me to find more information on the subject. I learned that I can control the scales of gray using for example the following:

levelplot(my_var, col.regions = gray(0:100/100))

which gives me 100 shades of gray from black (0) to white (1).

I'm using the function to plot gray scale images (photos) which I've pre-processed to a double matrix. I don't know if it's the best possible approach, but so far it works and I believe it gives me more options for graphing than the basic displaying options in the EBImage and rimage libraries. I don't know how I'd alter the palette to match displaying color images, but I'm glad I didn't have to do that so far...

user442446
  • 1,099
  • 3
  • 12
  • 13
  • If you have 256 levels of gray in your image (8bit), maybe gray(0:255/255) would be closer to the original? – Greg Sep 14 '10 at 23:39
  • Hey, a good tip! Although in the case of my images I stop seeing differences with values up from 50. – user442446 Sep 15 '10 at 10:15
5

There are many ways to accomplish your request. The simplest is:

trellis.device(color = FALSE)

another is

ltheme <- canonical.theme(color = FALSE) ## in-built B&W theme 
ltheme$strip.background$col <- "transparent" ## change strip bg 
lattice.options(default.theme = ltheme) ## set as default 

See this mail archive item: Re: [R] specify lattice black-and-white theme for more info.

Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
Greg
  • 11,564
  • 5
  • 41
  • 27
2

The only one that worked for me is:

>trellis.par.set(canonical.theme(color = FALSE)) 
Carla
  • 21
  • 1
2

You may also want to consider using panel.levelplot.raster, as it reduces the figure size sizably, as I recently found out. Combining this tip with changing trellis settings in general, here are a following examples:

trellis.par.set(regions=list(col=topo.colors(100)))
levelplot(volcano, panel = panel.levelplot.raster)

levelplot(volcano, panel = panel.levelplot.raster,
          par.settings=list(regions=list(col=topo.colors(100))))

The second method is deceptive because the trellis settings are in fact being changed globally. I didn't know about the col.regions argument though - that's pretty nice as it seems to change the color theme locally.

hatmatrix
  • 42,883
  • 45
  • 137
  • 231