[R] Using split.screen with a definition matrix of the screens seems to be a mess. I don't understand!
Julio Sergio Santana
juliosergio at gmail.com
Wed Mar 18 02:41:35 CET 2015
I have a particular need to divide the device space to draw different plots
and texts, so I decided to use split.screen using a matrix to define the
different space partitions.
My code and explanation is as follows:
# ------ START OF R CODE ----
dirGraf <- "TEST/" # A directory to put the result
nc <- 4 # number of data screens
tit <- "My title"
righttext <- "right text"
lefttext <- "left text"
# To ensure there are no graphics devices on:
graphics.off()
# I want to create 7 plotting windows (screens) with the following
# arrangement
# I'm supposing the NDC coordinates are such that the lower-left corner
# is at (0,0) and the upper-right corner is at (1,1). Am I right?
#
# +-----------------------------------+ (1,1)
# | 5 h=0.5/5.5 |
# +--+-----------------------------+--+
# | | 1 h=1.0/5.5 | |
# | +-----------------------------+ |
# | | 2 h=1.0/5.5 | |
# | +-----------------------------+ |
# | | 3 h=1.0/5.5 | |
# |6 +-----------------------------+ 7|
# | | | |
# | | 4 h=2.0/5.5 | |
# | | | |
# +--+-----------------------------+--+
# (0,0)
#
# I want 4 information (central) windows, from 1 to 4. However these windows
# must vary in their heights, because window 4 is twice the
# height the others.
# I want an upper window to put a title to the whole plot, and two
# lateral windows to add some texts.
#
# So I will build a four-column matrix, as instructed by the split.screen
# documentation. Each column correspond in order to the following
# characteristics, of each window (represented by each row), namely:
# 1. left, 2. right, 3. bottom, 4. top.
#
# First I will create a vector with the first four windows heights
Yinc <- c(2, rep(1,3))/5.5
# To have the NDC device coordinates (?) I will accumulate:
Yinc <- Reduce('+', Yinc, accumulate=T)
# To put this information in the right window order I invert the vector
Yinc <- Yinc[4:1]
# Then I will build the matrix for the first four windows:
Mm <- cbind(left=1/6, right=5/6, bottom=c(Yinc[2:4],0), top=Yinc)
# Now, let's add upper and lateral windows (screens 5 to 7)
Mm <- rbind(Mm,
# +--------+--------+--------+--------+
# | left | right | bottom | top |
# +--------+--------+--------+--------+
c( 0 , 1 , Yinc[1], 1 ),
c( 0 , 1/6 , 0 , Yinc[1]),
c( 5/6 , 1 , 0 , Yinc[1])
)
# margins for most of the cases
gpar <- list(mar=c(0,2.1,0,0))
# the device name (a pdf file)
gname <- paste0(dirGraf, "Test.pdf")
# Test table to plot, just a line from (0,0) to (1,1)
tt <- data.frame(x=c(0,1), y=c(0,1))
# Let's open the device:
pdf(gname, width=7, height=9.11)
# Let's split the device space (I think this is the default, isn't it?)
split.screen(Mm)
# Let's put a title in the upper window
screen(5)
par(gpar)
plot(c(0,1), c(0,1), ylab="", axes=F, type="n")
text(0.5, 0.5, tit, cex=1.5)
# For each "data" window
for (ii in 1:nc) {
screen(ii) # select screen 1 to 4
par(gpar)
plot(tt, ylab=LETTERS[ii], xlab="", type="l", xaxt="n")
}
# A text in the left window
screen(6)
par(mar=c(0.1,0.1,0.1,0.1))
plot(c(0,1), c(0,1), axes=F, type="n")
text(0.5, 0.5, lefttext, srt=90, cex=1.2)
# A text in the right window
screen(7)
par(mar=c(0.1,0.1,0.1,0.1))
plot(c(0,1), c(0,1), axes=F, type="n")
text(0.5, 0.5, righttext, srt=90, cex=1.2)
# close graphics
graphics.off()
# ------ END OF R CODE ----------------
At the end I have a totally messy pdf file with the four main windows
at the top and with no difference in their heights, and the other windows
in any place R (not me) chose to put them.
Furthermore the R interpreter issues a message:
"In par(new = TRUE) : call par(new=TRUE) without graphic"
Do you have any comments on this?
Thanks a lot!
-Sergio.
More information about the R-help
mailing list