1

I typed in the following:

histogram(~ Raw.no10$Width | Raw.no10$Station * Raw.no10$Year,
  data = Raw.no10, layout = c(4, 2), nin = 30, xlab = "Prosomal Width (mm)",
  strip = strip.custom(bg = 'white'), ylab = "Frequencies", tick = -1, col = 'grey') 

On the top of each small graph, it shows "Raw.no10$Year" instead of a specific year, such as 2014.

So, I changed to the following:

histogram(~ Raw.no10$Width | Raw.no10$Station * Raw.no10$Year,
  data = Raw.no10, layout = c(4, 2), nin = 30, xlab = "Prosomal Width (mm)",
  strip = strip.custom(bg = 'white', var.name = c("2002", "2014")),
  ylab = "Frequencies", tick = -1, col = 'grey')

The top of each small graph now shows "2014" exclusively. I can't find "2002" in any graphs.

How can I print the value of a conditioning variable in the strip above each panel in these lattice histograms?

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • 2
    What does `Raw.no10` look like? Does it reside in an existing package? Otherwise can you post the result of `dput(Raw.no10)`, or if too large, something like `head(Raw.no10)`? That way you'll be much more likely to get the help you're looking for. Also have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dominic Comtois Apr 06 '15 at 11:59
  • 1
    One right off the hip: Try calling variables, not vectors (this is why `data` argument is there for). `histogram(~Width | Station * Year, data=Raw.no10...`. – Roman Luštrik Apr 06 '15 at 12:58
  • Thank you for your suggestion, Roman.I have tried to do so and hence only the word "Year" shows consistently on all lattice plots....orz – Christine Lee Apr 06 '15 at 16:42
  • THanks Dominic. May I please know how to use the dput(Raw.no10)? I have tried to type in this programme language into the R and R said: "Error in dput(head(Raw.no10), 20) : 'file' must be a character string or connection " – Christine Lee Apr 06 '15 at 16:43
  • That should be `dput(head(Raw.no10, 20))` since the `20` is an argument to `head`. Then copy and paste what appears in the console after typing in that command. – BenBarnes Apr 08 '15 at 06:59

1 Answers1

1

The lattice plot functions will print the level of the conditioning variables (variables after the |) above the panels as long as the conditioning variables are factors. If the conditioning variables are numeric, which is my guess in your case, the values will not be printed automatically.

So there are two ways to go about answering your request

First, some reproducible data

set.seed(123)
histDat <- expand.grid(Station = 1:5, Year = 2009:2014)
histDat$Width <- rnorm(nrow(histDat), 15 + histDat$Station, 5)

1. Change the strip.levels argument to strip.custom

Pass the value c(TRUE, TRUE) to the strip.levels argument in the strip.custom function. This will print the levels of the conditioning variables whether or not the underlying variable in your data frame is a factor. It will also print the variable names.

histogram(~ Width | Station * Year, data = histDat,
    strip = strip.custom(bg = 'white', strip.levels = c(TRUE, TRUE)),
    ylab = "Frequencies" ,tick = -1, col = 'grey')

enter image description here 2. Change the conditioning variable to a factor

You can change the conditioning variable (Year in theis case) to a factor either by altering your data.frame or changing the formula passed to histogram as below. This will print the variable level but not the variable name. If you want the variable name, then you should change the value passed to the strip.names argument of strip.custom.

histogram(~ Width | Station * factor(Year), data = histDat,
    strip = strip.custom(bg = 'white', strip.names = c(TRUE, TRUE)),
    ylab = "Frequencies" ,tick = -1, col = 'grey')

enter image description here

BenBarnes
  • 19,114
  • 6
  • 56
  • 74