I'd like to add a subplot to an existing plot in R. The subplot (inset) should have a different background color. I tried the following:
#install.packages("TeachingDemos", dependencies=T)
library(package="TeachingDemos")
d0 <- data.frame(x = rnorm(150, sd=5), y = rnorm(150, sd=5))
d0_inset <- data.frame(x = rnorm(1500, sd=5), y = rnorm(1500, sd=5))
plot(d0)
subplot(
fun = plot(
d0_inset
, col = 2
, pch = '.'
, mgp = c(1,0.4,0)
, ann = F
, cex.axis=0.5
)
, x = grconvertX(c(0.75,1), from='npc')
, y = grconvertY(c(0,0.25), from='npc')
, type = 'fig'
, pars = list(
mar = c(1.5,1.5,0,0) + 0.1
, bg = "blue" # this should change the background color
)
)
In the help of subplot()
it says for pars
:
a list of parameters to be passed to par before running
fun
.
It seems to be very difficult to change the backgroundcolor of a plot, since the graphic parameter has a different meaning in plot()
. So one has to set the background color using par()
. but why does this not work for subplot
? (I also tried to put the plot-function into a extaernal function that calls par()
and plot()
, but this did not help).
Why does subplot not work properly?
The
bg
argument ofpar
change the background color of the device not of the plot. Since you're just merely adding a plot to an already opened and used device, this is not a possibility (because of the pen-and-paper waybase
plots are drawn). Instead what you can do (building on this previous answer) is the following:Edit: if you want the area containing also the annotations to be blue, the other solution i see would be to draw the rectangle prior to drawing the subplot (using the coordinates you gave to the subplot function):