如何在R的split.screen模式下更改屏幕大小?

时间:2013-07-09 16:12:07

标签: r layout plot split

我想在一台设备上绘制两个彼此相邻的图。我使用split.screen,因为它很容易处理。但是,似乎split.screen()平分屏幕。

在我的情况下,我想让正确的专栏略微缩小。这是因为我省略了轴标签,这使得右图比左图更宽:

#install.packages("TeachingDemos", dependencies=TRUE)
library(package="TeachingDemos")
# ***********************************************************
#source("subplot_new.R.txt")
# ***********************************************************
# data for all 4 plots
dx = c(1:10)
dy = c(10:1)


toPDF = T


if(toPDF) {
  pdf(file="test.pdf", width=10, height=5)
}
# split the screen into two columns
close.screen(all.screens=T)
split.screen(figs=c(1,2))

# go to screen 1
# ***********************************************************
screen(1)
par(mar=c(4,4,0.1,0.1), las=1)
plot(x = dx, y = dy)


# determine coordinates of subplot region:
x_left_right = grconvertX(c(0,0.5), from='npc')
y_bottom_top = grconvertY(c(0,0.5), from='npc')
# draw rectangle
rect(
  xleft     = x_left_right[1]
  , xright  = x_left_right[2]
  , ybottom = y_bottom_top[1]
  , ytop    = y_bottom_top[2]
  , col = "#efefef"
)
# add a subplot in green
subplot(
  fun={
    plot(x = dx, y = dy, ann = F)
  }
  , type = "fig"
  , x = x_left_right
  , y = y_bottom_top
  , pars = list(
    fg = "green"
    , mar = c(2,2,0,0)
    )
)

# go to screen 2
# ***********************************************************
screen(2)
par(mar=c(4,1,0.1,0.1), las=1)
plot(x = dx, y = dy, yaxt="n")
axis(side=2, labels=F)

# determine coordinates of subplot region:
x_left_right = grconvertX(c(0.6,1), from='npc')
y_bottom_top = grconvertY(c(0.6,1), from='npc')
# draw rectangle
rect(
  xleft     = x_left_right[1]
  , xright  = x_left_right[2]
  , ybottom = y_bottom_top[1]
  , ytop    = y_bottom_top[2]
  , col = "#efefef"
)
# add a subplot in blue
subplot(
  fun={
    plot(x = dx, y = dy, ann = F)
  }
  , type = "fig"
  , x = x_left_right
  , y = y_bottom_top
  , pars = list(fg = "blue", mar = c(2,2,0,0))
)

if(toPDF) {
  dev.off()
}

我认为layout可能有用,但可能不适合我,因为我需要使用TeachinDomes包中的subplot命令。

是否有类似于split.screen的包,允许设置每列应从整个屏幕获得的比例?或者是否可以操作split.screen包tofulfil任务?

我只是想让这两个地块具有相同的尺寸,而没有额外的白边距。

1 个答案:

答案 0 :(得分:9)

split.screen实际上非常灵活,让您可以自由控制屏幕的大小和位置。您需要做的就是为它提供一个N×4矩阵,其中每一行指定左,右,上和下边缘的相对位置。

有关详细信息,请参阅?split.screen

split.screen(matrix(c(0,.7,  .7,1,  0,0,  1,1), ncol=4))
screen(1)
plot(1, 1)
split.screen(matrix(c(0,0,  1,1,  0,.7,  .4,1), ncol=4), screen=2)
screen(3)
plot(2,2)
screen(4)
plot(3,3)

enter image description here

你甚至可以这样旅行,

split.screen(matrix(c(0:2,  4:6,  0:2,  4:6)/6, ncol=4))
screen(1)
plot(1,1)
screen(2)
plot(2,2)
screen(3)
plot(3,3)

enter image description here