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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:15:49+00:00 2026-06-15T06:15:49+00:00

When I make an experimental design, I use ggplot to show the layout. Here’s

  • 0

When I make an experimental design, I use ggplot to show the layout. Here’s a simple example:

df <- data.frame(Block=rep(1:2, each=18),
            Row=rep(1:9, 4),
            Col=rep(1:4, each=9),
            Treat=sample(c(1:6),replace=F))

Which I’ll plot like:

df.p <- ggplot(df, aes(Row, Col)) + geom_tile(aes(fill=as.factor(Treat)))

to give:

design

Sometimes I have a structure within the design I would like to highlight by putting a box around it, for example a mainplot. In this case:

df$Mainplot <- ceiling(df$Row/3) + 3*(ceiling(df$Col/2) - 1)

I then use geom_rect and some messy code that needs adjusting for each design to generate something like:

designwithmainplot

Question: How do I add the rectangles around the mainplots in a simple way? It seems like a simple enough problem, but I haven’t found an obvious way. I can map colour or some other aesthetic to mainplot, but I can’t seem to surround them with a box. Any pointers greatly appreciated.

  • 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-15T06:15:50+00:00Added an answer on June 15, 2026 at 6:15 am

    Here is a possible solution where I create an auxiliary data.frame for plotting borders with geom_rect(). I’m not sure if this is as simple as you would like! I hope the code that computes the rectangle coordinates will be reusable/generalizable with just a bit of additional effort.

    library(ggplot2)
    
    # Load example data.
    df = data.frame(Block=rep(1:2, each=18),
                Row=rep(1:9, 4),
                Col=rep(1:4, each=9),
                Treat=sample(c(1:6),replace=F))
    df$Mainplot = ceiling(df$Row/3) + 3*(ceiling(df$Col/2) - 1)
    
    # Create an auxiliary data.frame for plotting borders.
    group_dat = data.frame(Mainplot=sort(unique(df$Mainplot)),
                           xmin=0, xmax=0, ymin=0, ymax=0)
    # Fill data.frame with appropriate values.
    for(i in 1:nrow(group_dat)) {
        item = group_dat$Mainplot[i]
        tmp = df[df$Mainplot == item, ]
        group_dat[i, "xmin"] = min(tmp$Row) - 0.5
        group_dat[i, "xmax"] = max(tmp$Row) + 0.5
        group_dat[i, "ymin"] = min(tmp$Col) - 0.5
        group_dat[i, "ymax"] = max(tmp$Col) + 0.5
    }
    
    
    p2 = ggplot() + 
         geom_tile(data=df, aes(x=Row, y=Col, fill=factor(Treat)), 
                   colour="grey30", size=0.35) +
         geom_rect(data=group_dat, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
                   size=1.4, colour="grey30", fill=NA)
    
    ggsave(filename="plot_2.png", plot=p2, height=3, width=6.5) 
    

    enter image description here

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

Sidebar

Related Questions

I have made a simple experimental language of my own. I want make Eclipse
I'm trying to make an experimental web application which minimises redundant data. I have
I am trying to make a simple experimental app. I'd like for it to
I'm trying to learn how to make & use packages in Java. I've experimented
Make it on Linux. The reason to use more than one version of Vim,
i make one class file for jar. and this class have use wurfl. and
I am developing a Google Chrome extension, and need to use the chrome.experimental.webRequest API.
In an experimental project I am working on I am trying to make a
Purpose We're designing a Latin square (sudoku-like sequence) for an experimental design that needs
I'm using the scrollview experimental feature of jQuery Mobile: <div class=carousel data-scroll=xp> ... my

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.