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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:18:13+00:00 2026-06-17T06:18:13+00:00

I have generated a figure that combines ggplot and base graphics: t <- c(1:(24*14))

  • 0

I have generated a figure that combines ggplot and base graphics:

t <- c(1:(24*14)) 
P <- 24
A <- 10 
y <- A*sin(2*pi*t/P)+20 
#*****************************************************************************
par(mfrow = c(2,1))
plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude")
aa <- par("mai")
plot.new()

require(gridBase)
vps <- baseViewports()
pushViewport(vps$figure)
pushViewport(plotViewport(margins = aa)) ## I use 'aa' to set the margins 
#*******************************************************************************
require(ggplot2)
acz <- acf(y, plot = FALSE)
acd <- data.frame(Lag = acz$lag, ACF = acz$acf)
p <- ggplot(acd, aes(Lag, ACF)) + geom_area(fill = "grey") +
  geom_hline(yintercept = c(0.05, -0.05), linetype = "dashed") +
  theme_bw()
grid.draw(ggplotGrob(p)) ## draw the figure

I use the plotViewport command and set the dimensions of the panel according to the dimensions of the first panel, obtained by par(“mai”). The figure attached shows the outcome.
enter image description here
However, the dimensions of both panels do not match, i.e. the second panel seems to be slightly wider than the first. How can I overcome this without having to manually set the margins with

pushViewport(plotViewport(c(4,1.2,0,1.2)))
  • 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-17T06:18:14+00:00Added an answer on June 17, 2026 at 6:18 am

    This should give you some hints:

    screenshot

    library(grid)
    library(ggplot2)
    require(gridBase)
    
    par(mfrow = c(2,1))
    plot(1:10)
    a <- par("mai")
    plot.new()
    vps <- baseViewports()
    pushViewport(vps$figure)
    
    p = qplot(1:10, 1:10) + theme_bw() 
    g <- ggplotGrob(p)
    
    lw = unit(a[2], "inch") - sum(g$widths[1:3]) 
    
    g$widths[[2]] <- as.list(lw + g$widths[[2]])
    g$widths[[4]] <- as.list(unit(1, "npc") - unit(a[2] + a[4], "inch"))
    g$widths[[5]] <- unit(a[4], "inch")
    grid.draw(g)
    
    # draw a shaded vertical band to test the alignment
    grid.rect(unit(a[2], "inch"), unit(0, "inch"), 
              unit(1,"npc") - unit(a[2] + a[4], "inch"), 
              unit(2,"npc"),
              gp=gpar(lty=2, fill="red", alpha=0.1), hjust=0, vjust=0)
    
    upViewport()
    

    but, really, why would you not do everything in ggplot2?

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

Sidebar

Related Questions

I have code that generates a List<string[]> variable but can't quite figure out how
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
I have a selection list that is generated dynamically, it lists a number of
I have a TimePicker in my app that can select the amount of time
I have a cookie which is generated from a servlet and that I would
I have a randomly generated 10x5 array that I want to check when 3
I have multiple arrays, that get generated when a for loop completes. Now I
I can't figure out what I'm doing wrong. I have dynamically generated input fields
I have several divs that are generated dynamically after a file is uploaded. Each
I have several expressions I have generated in Mathematica that I would like to

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.