split.screen, screen

The split.screen function is used to split a plotting window into different rectangular sections called "screens". This is similar to what par(mfrow) does, but it is more general since it allows for some plots to span rows and columns. The screen function is used to select a screen for adding a plot and screen also makes it possible to move back and forth between the screens.
split.screen(figs, screen)
  • figs – Describe the number of rows and columns for a new plot via a numeric vector of length 2. Or, if the screen argument is also set, then specify how that specified screen should be split.
  • screen – If a screen is to be split into pieces, specify the screen number.
screen(n)
  • n – The number of the screen to which plotting commands should be assigned.

Example. A plot with three screens is generated below. The first step is to generate a window with two screens in the form of 2 rows and 1 column. Then the top screen is split into two smaller screens. Notice that the screen numbers are always integers and correspond to the order in which the screens were created. For instance, the first split creates screens 1 and 2, then the second split generates screens 3 and 4. To generate a plot for a screen, that screen must first be selected, as shown with the scatterplot and two histograms.
> png("screens.png")
> 
> #______ Simulate Data ______#
> set.seed(5)
> x <- rnorm(100, 50, 25)
> y <- -0.3*x^2 + 25*x + 92 + rnorm(100, sd=300)
> 
> #______ Window With 2 Rows, Top Row In 2 Columns ______#
> split.screen(c(2,1)) # Makes Screen 1 and 2
[1] 1 2
> split.screen(c(1,2), screen=1) # Makes Screen 3 and 4
[1] 3 4
> 
> #______ Output On Screen 2 ______#
> screen(2)
> plot(x,y)
> 
> #______ Histograms on Screens 3 and 4 ______#
> screen(3)
> hist(x)
> screen(4)
> hist(y)
> 
> dev.off()
null device 
          1 

split.screen and screen examples for R

There is also a erase.screen function, which we do not discuss here; if this may be of use to you, first read the Details section of the help file. The layout function is yet more general than split.screen but doesn’t allow for as interactive of an experience.

Leave a Reply