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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:47:15+00:00 2026-06-14T01:47:15+00:00

I have the same problem as this user – I have a ‘jagged’ faceted

  • 0

I have the same problem as this user – I have a ‘jagged’ faceted plot, in which the bottom row has fewer panels than the other rows, and I would like to have x-axis ticks on the bottom of each column.

The suggested solution for that problem was to set scales="free_x". (In ggplot 0.9.2.1; I believe the behavior I’m looking for was default in earlier versions.) That’s a poor solution in my case: my actual axis labels will be rather long, so putting them under each row will occupy too much room. The results are something like this:

 x <- gl(3, 1, 15, labels=paste("this is a very long axis label ", letters[1:5]))
 y <- rnorm(length(x))
 l <- gl(5, 3, 15)
 d <- data.frame(x=x, y=y, l=l)
 ggplot(d, aes(x=x, y=y)) + geom_point() + facet_wrap(~l, scales="free_x") + 
   theme(axis.text.x=element_text(angle=90, hjust=1))

enter image description here

In a comment here, Andrie suggests that it can be done manually in grid but I have no idea how to get started on that.

  • 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-14T01:47:16+00:00Added an answer on June 14, 2026 at 1:47 am

    If I remember right, there were questions on both how to add all labels to the same line under the last column and how to lift these last labels up to the next row. So here is the function for both cases:

    Edit: since this is like a substitute for print.ggplot (see getAnywhere(print.ggplot)) I have added some lines from it to preserve functionality.

    Edit 2: I have improved it a bit more: no need to specify nrow and ncol anymore, plots with all the panels can be printed too.

    library(grid)
    # pos - where to add new labels
    # newpage, vp - see ?print.ggplot
    facetAdjust <- function(x, pos = c("up", "down"), 
                            newpage = is.null(vp), vp = NULL)
    {
      # part of print.ggplot
      ggplot2:::set_last_plot(x)
      if(newpage)
        grid.newpage()
      pos <- match.arg(pos)
      p <- ggplot_build(x)
      gtable <- ggplot_gtable(p)
      # finding dimensions
      dims <- apply(p$panel$layout[2:3], 2, max)
      nrow <- dims[1]
      ncol <- dims[2]
      # number of panels in the plot
      panels <- sum(grepl("panel", names(gtable$grobs)))
      space <- ncol * nrow
      # missing panels
      n <- space - panels
      # checking whether modifications are needed
      if(panels != space){
        # indices of panels to fix
        idx <- (space - ncol - n + 1):(space - ncol)
        # copying x-axis of the last existing panel to the chosen panels 
        # in the row above
        gtable$grobs[paste0("axis_b",idx)] <- list(gtable$grobs[[paste0("axis_b",panels)]])
        if(pos == "down"){
          # if pos == down then shifting labels down to the same level as 
          # the x-axis of last panel
          rows <- grep(paste0("axis_b\\-[", idx[1], "-", idx[n], "]"), 
                       gtable$layout$name)
          lastAxis <- grep(paste0("axis_b\\-", panels), gtable$layout$name)
          gtable$layout[rows, c("t","b")] <- gtable$layout[lastAxis, c("t")]
        }
      }
      # again part of print.ggplot, plotting adjusted version
      if(is.null(vp)){
        grid.draw(gtable)
      }
      else{
        if (is.character(vp)) 
          seekViewport(vp)
        else pushViewport(vp)
        grid.draw(gtable)
        upViewport()
      }
      invisible(p)
    }
    

    And here is how it looks

    d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
      xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) + 
      facet_wrap(~ color)
    facetAdjust(d)
    

    enter image description here

    facetAdjust(d, "down")
    

    enter image description here

    Edit 3:

    This is an alternative solution, the one above is fine as well.

    There are some issues when one wants to use ggsave together with facetAdjust. A plot of class of ggplot is required because of two parts in the source code of ggsave: print(plot) and default_name(plot) in case one does not provide a filename manually (according to ?ggsave it seems that it is not supposed to work, though). Hence, given a filename, there is a workaround (possibly with side effects in some cases):

    First, let us consider the separate function that achieves the main effect of floating axis. Normally, it would return a gtable object, however we use class(gtable) <- c("facetAdjust", "gtable", "ggplot"). In this way, it is allowed to use ggsave and print(plot) works as required (see below for print.facetAdjust)

    facetAdjust <- function(x, pos = c("up", "down"))
    {
      pos <- match.arg(pos)
      p <- ggplot_build(x)
      gtable <- ggplot_gtable(p); dev.off()
      dims <- apply(p$panel$layout[2:3], 2, max)
      nrow <- dims[1]
      ncol <- dims[2]
      panels <- sum(grepl("panel", names(gtable$grobs)))
      space <- ncol * nrow
      n <- space - panels
      if(panels != space){
        idx <- (space - ncol - n + 1):(space - ncol)
        gtable$grobs[paste0("axis_b",idx)] <- list(gtable$grobs[[paste0("axis_b",panels)]])
        if(pos == "down"){
          rows <- grep(paste0("axis_b\\-[", idx[1], "-", idx[n], "]"), 
                       gtable$layout$name)
          lastAxis <- grep(paste0("axis_b\\-", panels), gtable$layout$name)
          gtable$layout[rows, c("t","b")] <- gtable$layout[lastAxis, c("t")]
        }
      }
      class(gtable) <- c("facetAdjust", "gtable", "ggplot"); gtable
    }
    

    The function for printing which differs only by few lines from ggplot2:::print.ggplot:

    print.facetAdjust <- function(x, newpage = is.null(vp), vp = NULL) {
      if(newpage)
        grid.newpage()
      if(is.null(vp)){
        grid.draw(x)
      } else {
        if (is.character(vp)) 
          seekViewport(vp)
        else pushViewport(vp)
        grid.draw(x)
        upViewport()
      }
      invisible(x)
    }
    

    Example:

    d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
      xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) + 
      facet_wrap(~ color)
    p <- facetAdjust(d) # No output
    print(p) # The same output as with the old version of facetAdjust()
    ggsave("name.pdf", p) # Works, a filename is necessary
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the same problem as the user in this question , which is
I have the exact same problem as this guy , since no one has
I have three tables that I use which has proved this problem to be
I have basically the same problem outlined in this question, however I am using
I have faced the same problem many times. The Same Problem was With This
I have read the other threads regarding this same problem but I still don't
This is pretty much the same problem i have, except with very different code:
I basically have the same problem in this questions: Flash Video still playing in
The problem in general is this: I have a page with content and user
I have this same problem, but the solution presented isn't working, nor is any

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.