How to change the background colour of a subplot/inset in R?

你离开我真会死。 提交于 2019-12-03 17:32:52
plannapus

The bg argument of par 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 way base plots are drawn). Instead what you can do (building on this previous answer) is the following:

plot(d0)
subplot(fun = {plot(d0_inset, mgp = c(1,0.4,0), ann = F, cex.axis=0.5);
               rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "blue");
               points(d0_inset, col=2, pch=".") }, 
        x = grconvertX(c(0.75,1), from='npc'), 
        y = grconvertY(c(0,0.25), from='npc'), 
        pars = list(mar = c(1.5,1.5,0,0) + 0.1), type="fig")

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):

plot(d0)
rect(grconvertX(0.75, from='npc'), grconvertY(0, from='npc'),
     grconvertX(1, from='npc'), grconvertY(0.25, from='npc'), 
     col="blue", border=NA)
subplot(fun = plot(d0_inset, mgp = c(1,0.4,0), ann = F, 
                    cex.axis=0.5,col=2, pch=".") , 
        x = grconvertX(c(0.75,1), from='npc'), 
        y = grconvertY(c(0,0.25), from='npc'), 
        pars = list(mar = c(1.5,1.5,0,0) + 0.1), type="fig")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!