Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:58:45+00:00 2026-06-06T02:58:45+00:00

I just tried my first steps with grid . I would like to set

  • 0

I just tried my first steps with grid. I would like to set up a 2 x 2 matrix of square scatter plots, with some space in between. To get the space, I actually use a 3 x 3 layout (Question 1: is there an easier way?). As you can see from the example below, the points are plotted outside the bounding rectangle. I somehow have to specify the limits in the plot. Question 2: How can this be done? Finally, can I use base graphics to create the plots? [I know lattice graphics will work and also a standard layout could be used (or ggplot2), but I am interested if that’s possible with grid.layout, too]

require(grid)

## generate data to be plotted in the top left plot
X <- matrix(rexp(2000), ncol=2)

## plot device
file <- "foo.pdf"
pdf(file=file, width=10, height=10)

## set up grid.layout
gl <- grid.layout(3, 3, respect=rbind(c(0,1,0), c(1,1,1), c(0,1,0)),
                  widths=unit(c(3,1,3), "inches"), heights=unit(c(3,1,3), "inches")) # define grid layout

pushViewport(viewport(layout=gl)) # use this layout in a viewport

## (1,1) plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1, name="11"))
grid.points(X[,1], X[,2], pch=1) # points
grid.rect() # bounding rectangle
grid.xaxis() # x-axis
grid.yaxis() # y-axis
grid.text(expression(italic(X[1])), y=unit(-3, "lines")) # x-axis label
grid.text(expression(italic(X[2])), x=unit(-3, "lines"), rot=90) # y-axis label
grid.text("Plot 1", x=0.86, y=0.9, gp=gpar(fontface="bold", cex=1.6)) # add label
upViewport()

## (1,2) plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=3, name="13"))
grid.rect()
upViewport()

## (2,1) plot
pushViewport(viewport(layout.pos.row=3, layout.pos.col=1, name="31"))
grid.rect()
upViewport()

## (2,2) plot
pushViewport(viewport(layout.pos.row=3, layout.pos.col=3, name="33"))
grid.rect()
upViewport()

## plot device
dev.off()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:58:46+00:00Added an answer on June 6, 2026 at 2:58 am

    You need to have the correct coordinate system: either convert your data to “npc” units, or set up a viewport with native units derived from your data,

    pushViewport(dataViewport(X[,1], X[,2], 
                   layout.pos.row=1, layout.pos.col=1, name="11"))
    

    works as expected.

    Regarding question 1, have a look at the gtable package on github; it is being developed to facilitate this sort of things for ggplot2 and other grid-based graphic frameworks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to load my Array s like this, but it loads just first
I've just started learning MATLAB, and the first thing i tried is to plot
Just tried some small graphics application of mine on Windows 7, and I'm getting
I just tried disassembling some shellcode using disasm and I found the following statement
I just tried the tooltip like this Easy image preview ... But i cant
I am just starting out with GIT (i'm coming from cvs) and would like
I set my first steps upon the noble path of using unit tests to
I've just took my first steps in mobile development with Titanium Mobile. Coming from
I have really tried to get my head around the first steps of understanding
I just tried to install git-flow, however, it does not seem to be integrated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.