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 8928697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:30:15+00:00 2026-06-15T08:30:15+00:00

I am arranging plots generated by ggplot2. I have to use print to print

  • 0

I am arranging plots generated by ggplot2. I have to use print to print out plots and grid.draw to display legend.

Sample Code:

p0 <- ggplot(data = iris, geom = 'blank',
         aes(y = Petal.Width, x = Petal.Length, color = Species)) + geom_point() +
      theme(axis.title.x = element_blank(),
            axis.title.y = element_blank(),
            legend.position = "none")

p1 <- ggplot(data = iris, geom = 'blank',
         aes(y = Petal.Length, x = Petal.Width, color = Species)) + geom_point() +
       theme(axis.title.x = element_blank(),
             axis.title.y = element_blank(),
             legend.position = "none")

g_legend <- function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

p <- ggplot(data = iris, geom = 'blank',
        aes(y = Petal.Width, x = Petal.Length, color = Species)) + geom_point()


grid.newpage() 
pushViewport(viewport(layout = grid.layout(2, 4)))
print(p0,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p0,vp = viewport(layout.pos.row = 1, layout.pos.col = 2:3))
print(p1,vp = viewport(layout.pos.row = 2, layout.pos.col = 2:3))
grid.text("This is x label",gp=gpar(fontsize = 14), vjust = 11,
          vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
grid.text("This is y label",gp=gpar(fontsize = 14), vjust = -11, rot = 90,
          ![enter image description here][1]vp = viewport(layout.pos.row = 2, layout.pos.col = 2))

grid.draw(g_legend(p))

I would like to put the legend at the fourth column. How can I do it? Thanks.

  • 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-15T08:30:17+00:00Added an answer on June 15, 2026 at 8:30 am

    enter image description hereusing gridExtra

             library(gridExtra)
             grid.arrange(p0 , p1,  g_legend(p), ncol=3,
             heights=c(10, 1),widths =c(1,2,1) ,as.table =TRUE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem regarding arranging of code, which depends on each other
I want help arranging images. I have this table and I want the images
I'm arranging elements on a layout, but if I use Linear Layout, everything lines
i have problem arranging the numbers from small to big. Im trying to overide
I'm facing the issue when arranging one array in PHP. Please help me out.
I am trying to figure out general guidelines for arranging views/templates in a django
I have an editable UITableView. While my user is re-arranging the cells, I want
As just like firefox re-arranging tabs functionality... is there any online javascript open-source code
I've started developing my code using the example from SO WPF: arranging collection items
Is there any default functionality for arranging JInternalFrames in Java Swing? I would like

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.