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

  • Home
  • SEARCH
  • 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 8609187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:49:10+00:00 2026-06-12T03:49:10+00:00

I’ve got a mostly cosmetic problem. I’m creating four plots using the ggplot2 library

  • 0

I’ve got a mostly cosmetic problem. I’m creating four plots using the ggplot2 library that I then arrange in one column (using this). The graphs display the same data but for four groups, the x-axis is time which is why I want to keep the graphs in one single column.

So I add the legend to the top graph, and the labels for the x-axis to the bottom graph. These two actions change the size of the graph; adding a legend causes the graph to grow, adding the x-axis labels causes it to shrink to accomodate these things.

Is there a way to specify a fixed graph size, that would make my layout consistent?

My plot:
plot

Code for reproducible results:

library(ggplot2)
library(reshape)

raw_data <- structure(list(Sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L), Month = structure(c(12L, 12L, 11L, 11L, 10L, 
10L, 3L, 3L, 5L, 5L, 4L, 4L, 8L, 8L, 1L, 1L, 9L, 9L, 7L, 7L, 
6L, 6L, 2L, 2L, 12L, 12L, 12L, 12L, 11L, 11L, 10L, 10L, 3L, 3L, 
5L, 5L, 4L, 4L, 8L, 8L, 1L, 1L, 9L, 9L, 7L, 7L, 6L, 6L, 2L, 2L, 
12L, 12L), .Label = c("April", "Aug", "Dec", "Feb", "Jan", "July", 
"June", "March", "May", "Nov", "Oct", "Sep"), class = "factor"), 
    Channel = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", 
    "B"), class = "factor"), Amplitude = c(5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L)), .Names = c("Sample", 
"Month", "Channel", "Amplitude"), row.names = c(NA, 52L), class = "data.frame")



multiplot <- function(..., plotlist=NULL, cols) {
    require(grid)

    # Make a list from the ... arguments and plotlist
    plots <- c(list(...), plotlist)

    numPlots = length(plots)

    # Make the panel
    plotCols = cols                          # Number of columns of plots
    plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols

    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
    vplayout <- function(x, y) {
            viewport(layout.pos.row = x, layout.pos.col = y)
    }

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
        curRow = ceiling(i/plotCols)
        curCol = (i-1) %% plotCols + 1
        print(plots[[i]], vp = vplayout(curRow, curCol))
    }

}


mybarplot <- function(first=0, last=0) {
    # Create the barplot 
    p <- ggplot(raw_data, aes(x=Sample, y=Amplitude, fill=Channel))

    # Make it a grouped barplot with already summarised values
    p <- p +  geom_bar(position="dodge", stat="identity")


    # Apply a log10 transformation to the y-axis, and create appropriate axis ticks
    p <- p + scale_y_log10(breaks = c(5,10,50,100,500,1000,5000,10000))

    # Zoom in (barplots will not show when axis change to remove 0, so have to zoom)
    p <- p + coord_cartesian(ylim=c(1,15000), xlim=c(1,26))

    # Make it greyscale
    p <- p + scale_fill_grey()


    # Hide X label
    p <- p + opts(axis.text.x=theme_blank(), axis.title.x=theme_blank(), axis.title.y=theme_blank())
    # Change X label size
    p <- p + opts(axis.text.y=theme_text(size=7))



    # Change the Legend
    p <- p + scale_fill_manual(values=c("black", "grey75", "grey25"), name="Channel", breaks=c("A", "B"))

    #margins
    # c(top,,bottom,)
    top_margin    <- unit(c(    1, 1, -0.25, 1), "lines")
    middle_margin <- unit(c(-0.25, 1, -0.25, 1), "lines")
    bottom_margin <- unit(c(-0.25, 1,     2, 1), "lines")


    if (first) {
        # Anchor legend box to top right corner
        p <- p + opts(legend.justification=c(1,1), legend.position=c(1,1))
        # Put a white box around it
        p <- p + opts(legend.background = theme_rect(fill="white"))
        # Top margin
        p <- p + opts(plot.margin = top_margin)
        p <- p + scale_x_discrete(breaks = 1:26)
    } else {
        p <- p + opts(legend.position="none")
        if (last) {
            # Bottom margin
            p <- p + opts(plot.margin = bottom_margin)
                # label X-axis
            p <- p + scale_x_discrete(breaks = 1:26, labels=c("Sep", "", "Oct", "", "Nov", "", "Dec", "", "Jan", "", "Feb", "", "March", "", "April", "", "May", "", "June", "", "July", "", "Aug", "", "Sep", ""))

            p <- p + ylab("Amplitude")
            p <- p + xlab("Sampling time")
            # Angle x labels
            #p <- p + opts(axis.text.x=theme_text(angle=-45, hjust=0.5))
            p <- p + opts(axis.text.x=theme_text(hjust=0.5))

            # Move X title
            p <- p + opts(axis.title.x=theme_text(vjust=-0.5))
        } else {
            p <- p + opts(plot.margin = middle_margin)
            p <- p + scale_x_discrete(breaks = 1:26)
        }
    }



}


plot1 <- mybarplot(first=1)
plot2 <- mybarplot()
plot3 <- mybarplot()
plot4 <- mybarplot(last=1)

multiplot(plot1, plot2, plot3, plot4, cols=1)

Session info:

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reshape_0.8.4 plyr_1.7.1    ggplot2_0.9.1

loaded via a namespace (and not attached):
 [1] MASS_7.3-18        RColorBrewer_1.0-5 colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       labeling_0.1       memoise_0.1        munsell_0.3        proto_0.3-9.2      reshape2_1.2.1    
[11] scales_0.2.1       stringr_0.6.1     
  • 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-12T03:49:12+00:00Added an answer on June 12, 2026 at 3:49 am

    In your example each plot is identical, however I assume that isn’t the plan for your final product. I think the easiest way to do this is with faceting rather than laying out each plot separately.

    dat <- data.frame(facetvar=letters[1:5], yvar=rep(1:10, each=5), xvar=rep(letters[6:10], each=5))
    ggplot(dat, aes(x=xvar, y=yvar, group=facetvar)) + 
        geom_bar(stat='identity') + 
        facet_grid(facetvar~.)
    

    You can subset your data first if need be and use an arbitrary faceting variable.

    ggplot(dat[sample(1:50, 40),], aes(x=xvar, y=yvar, group=facetvar)) + 
        geom_bar(stat='identity') + 
        facet_grid(facetvar~.)
    

    You can also supply scales.y='free' to facet_grid() if needed.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.