Julio Sergio Santana
2015-Mar-18 01:41 UTC
[R] Using split.screen with a definition matrix of the screens seems to be a mess. I don't understand!
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.
Jim Lemon
2015-Mar-18 09:02 UTC
[R] Using split.screen with a definition matrix of the screens seems to be a mess. I don't understand!
Hi Sergio, In order to get this configuration you will have to do multiple splits. Try this to see how you get what you want, and notice that screens 2 and 4 are now unusable as they have been split to make more screens. split.screen(figs=matrix(c(0,1,0.91,1,0,1,0,0.91),nrow=2,byrow=TRUE)) split.screen(figs=matrix(c(0,0.09,0,1,0.09,0.91,0,1,0.91,1,0,1),nrow=3, byrow=TRUE),screen=2) split.screen(figs=matrix(c(0,1,0.8,1,0,1,0.6,0.8,0,1,0.4,0.6,0,1,0,0.4), nrow=4,byrow=TRUE),screen=4) screen(1) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,1) screen(3) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,3) screen(5) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,5) screen(6) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,6) screen(7) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,7) screen(8) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,8) screen(9) par(mar=c(0,0,0,0)) plot(1:2,type="n",axes=FALSE) box() text(1.5,1.5,9) Jim On Wed, Mar 18, 2015 at 12:41 PM, Julio Sergio Santana < juliosergio at gmail.com> wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]