1

I want to illustrate R's par() graphics parameter command with multiple graphs, so I did a simple 2×2 layout that's graphed great. I added a single par (col = "green") command to cause the one barplot() and three hist()ograms, but it did nothing that I could see.

Here's my R script, which should be safe since I save and restore your graphics settings at the top and bottom. Apologies for the long dput() but I want you to have the data I have.

    savedGraphicsParams <- par(no.readonly=TRUE)
    layout(matrix(c(1, 2, 3, 4), nrow=2, byrow=TRUE))
    par(col = "green") # doesn't work

    attach(Lakes)

    # GRAPH 1:
    barplot(table(N_of_Fish), main="Fish", xlab = "No. of Fish")

    # GRAPH 2:
    hist(Elevation, main = "Elevation", xlab = "ft")

    # GRAPH 3
    hist(Surface_Area, main="Surface Area", xlab = parse(text="ft^2"))

    # GRAPH 4
    hist(`, main="Max Depth", xlab = "ft")

    detach(Lakes)     

par(savedGraphicsParams) # Reset the graphics
Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

0

tl;dr Unfortunately, as far as I know, you just can't do this; you have to use col= in the individual plot calls. Picking through ?par, we find:

Several parameters can only be set by a call to ‘par()’: ...
The remaining parameters can also be set as arguments (often via ‘...’) to high-level plot functions ...
However, see the comments on ‘bg’, ‘cex’, ‘col’, ‘lty’, ‘lwd’ and ‘pch’ which may be taken as arguments to certain plot functions rather than as graphical parameters.

(emphasis added).

I interpret this as meaning that bg et al. cannot be set globally by a call to par() (even though they're described and discussed in ?par), but must be set as arguments to individual plotting calls. I would write the code this way (also avoiding the use of attach(), which is advised against even in its own manual page ...)

plot_col <- "green"
with (Lakes,
 {
  barplot(table(N_of_Fish), main="Fish", xlab = "No. of Fish", col=plot_col)
  hist(Elevation, main = "Elevation", xlab = "ft", col=plot_col)
  hist(Surface_Area, main="Surface Area", xlab = parse(text="ft^2"), col=plot_col)
  hist(Maximum_Depth, main="Max Depth", xlab = "ft", col=plot_col)
 })
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks, Ben. I misinterpreted your bold sentence. When `bg` worked as a global parameter, I thought `col` would as well, especially since all the other col versions worked. I've read all the arguments on `attach` and `with` and the consensus is not to use them. I think they're both appropriate in short blocks of example code when only one data table is involved, especially if you detach immediately. – Sciolism Apparently Feb 21 '19 at 02:18
  • I agree that it's not worth being dogmatic about anything, including `attach()`. I'm surprised that you say that there's a consensus not to use `with()` though - where did you read that? If my answer resolves (I won't say "solves") your question, you can click the check-mark to accept it ... – Ben Bolker Feb 21 '19 at 04:05
  • I’m trying to remember the last R book I read. They’re all the same it seems—nothing that creates local scoping is good. Datatable$Variable is about all that’s approved of in the “literature” these days. – Sciolism Apparently Feb 21 '19 at 04:07
  • huh. Still find that surprising. Lots of interest in tidyverse (which uses non-standard evaluation to avoid $), but I'd be interested if you can point me to a source that deprecates `with()` ... – Ben Bolker Feb 21 '19 at 17:11
  • Deprecates? I have no source. Just recommendations. – Sciolism Apparently Feb 21 '19 at 17:25
  • By "deprecates" I simply mean "advocates not using" (or, quoting your previous comment, says that "nothing that creates local scoping is good". The usual advice (e.g. [here](https://stackoverflow.com/questions/40242904/problems-with-attach-in-r) or [here](https://stackoverflow.com/questions/5797797/attach-inside-function/5800785#5800785) is to use `with()`. But maybe I'm out of date/out of touch - that's why I'm curious where you're finding the recommendations to avoid local scoping at all costs ... – Ben Bolker Feb 21 '19 at 23:21
  • 1
    I can't find the source, and I've looked all afternoon. I'll start going with with() as the official good news. – Sciolism Apparently Feb 21 '19 at 23:39
  • I was hoist by my own with statement today, as I used it as a shortcut to generate a sheet full of diagnostic plots from the same data set. Later, I needed one of the return values of that function, and couldn’t (for the moment) get it, because I forget that with() groups, but also locally scopes. – Sciolism Apparently Mar 06 '19 at 07:11