如何在R中更改子图/插图的背景颜色? [英] How to change the background colour of a subplot/inset in R?

查看:554
本文介绍了如何在R中更改子图/插图的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向R中的现有图添加一个子图.该子图(插图)应具有不同的背景色.我尝试了以下方法:

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

subplot()的帮助下表示pars:

在运行fun之前要传递给par的参数列表.

a list of parameters to be passed to par before running fun.

更改图形的背景色似乎非常困难,因为图形参数在plot()中具有不同的含义.因此,必须使用par()设置背景色.但是为什么这对于subplot不起作用? (我还尝试将plot函数放到一个调用par()plot()的外部函数中,但这无济于事.)

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?

推荐答案

parbg自变量更改设备的背景颜色而不是绘图的背景颜色.由于您只是将绘图添加到已打开并已使用过的设备中,因此这是不可能的(因为使用笔和纸方式绘制base绘图).相反,您可以做的事情(基于此先前的答案)是:

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

这篇关于如何在R中更改子图/插图的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆