Reputation: 11082
I want to draw two plots next to each other on a single device. I use split.screen
, since it is easy to handle. But, it seems that split.screen()
divides the screen equally.
In my case I'd like to have the right column to be slightly narrower. This is because I leave out the axis labels, , which makes the right plot a bit wider than the left plot:
#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()
}
I thought layout
might work, but probably not in my case, since I need to use the subplot command from the TeachinDomes package.
Is there a package similar to split.screen
that allows for setting the proportion each column should get from the whole screen? Or is it possible to manipulate the split.screen package tofulfil the task?
I simply want to have the two plots having the same size, without having additional white margins.
Upvotes: 3
Views: 4209
Reputation: 14842
split.screen
is in fact very flexible and let's you control the size and position your screens freely. All you need to do is feed it an N-by-4 matrix where each row specify the relative position of the left, right, top and bottom edges.
See ?split.screen
for more details.
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)
You can even trip out like this,
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)
Upvotes: 11