1

Sometimes, as in the following code, I call the same boxplot several times.

x <- 1:10
y <- 1:5
r <- boxplot(x, y,col="blue")
grid(nx=NA, ny=NULL) #grid over boxplot
par(new=TRUE)
boxplot(x, y,col="blue")#grid behind boxplot

In cases with many boxplot-parameters (unlike here), this generates many lines of code.

How can I use the variable r for the second call in order to save this space?

Clyde Frog
  • 483
  • 1
  • 6
  • 15

1 Answers1

2

Boxplot returns a list which is now in variable r. You can plot it using bxp(r), to get the boxplot again.

For example,

bxp(r)

As it doesn't store all parameters, one option would be to store them separately... and call them when plotting. In addition to color, I've stored main and cex.axis

lst <- list(z = r, boxfill = "blue", cex.axis = 2, 
        main = "nice title")

do.call("bxp", lst)
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
  • I see, thanks. However, the variable r does not contain the information on colours, position of labels etc. So when using "bxp(r)" in a more elaborate example, the two boxplots don't match. Do you know a way to exactly call the same boxplot again without using all the code? – Clyde Frog Feb 17 '18 at 12:34
  • if you look at `str(r)`, it doesn't store such information. So, your best bet would be to write a function that wraps the boxplot function. Alternatively, you can use something like `ggplot2`. – kangaroo_cliff Feb 17 '18 at 13:05
  • @user3451767 See the new solution. If something like this is good enough, let me know what else you want to pass the boxplot in the `lst`, in case if that was not easy todo. – kangaroo_cliff Feb 17 '18 at 13:45