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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:35:53+00:00 2026-06-09T10:35:53+00:00

I’ve run two experiments, some replicating the conditions of a previous set. I have

  • 0

I’ve run two experiments, some replicating the conditions of a previous set. I have a column for the unique lot ID as well as another column containing the ID from experiment 1 which the lots from experiment 2 replicates. Here’s some example data stored like this:

test <- data.frame(var1=c(rep("A",4), rep("B",4), rep("C",4), rep("D",4)),
                   var2=rep(c(rep("A",4), rep("B",4)),2),
                   value=runif(16,1,5))

Here’s my ggplot code:

ggplot(test, aes(x=var1, y=value, fill=var2)) + geom_boxplot()

This gives me the lot IDs arranged according to var1‘s factor order.

enter image description here

I’d like the A’s from var2 side by side and the B’s from var2 side by side. Is the only way to do this by using facet_grid or facet_wrap?

 ggplot(test, aes(x=var1, y=value, fill=var2)) + geom_boxplot() +
        facet_grid(. ~ var2, scales="free_x")

enter image description here

I tried adding group=var2 but that gives me overlapping and very wide boxplots, which I also don’t understand:

ggplot(test, aes(x=var1, y=value, group=var2, fill=var2)) + geom_boxplot()
Warning message:
position_dodge requires non-overlapping x intervals

enter image description here

I can use facetting; I’m mostly asking the question as I was surprised when I couldn’t group how I expected. I also looked in the examples for geom_bar() for more grouping approaches but it seems most people aren’t grouping things this way.

Feel free to provide other input on how one might approach this. I’m simply looking to compare pairs of test results to make it easy to see whether the replicate matches the original trial.

  • 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-09T10:35:56+00:00Added an answer on June 9, 2026 at 10:35 am

    A cheats method using interaction and adjusting the scale_x_discrete labels

     ggplot(test, aes(x=interaction(var1,var2), y=value, fill=var2)) + 
      geom_boxplot() +
      scale_x_discrete(name = 'var1',breaks = c('A.A','C.A','B.B','D.B'), 
                       labels = c('A','C','B','D')) 
    

    enter image description here

    EDIT Thanks to @Andrie’s comments

    Or you can create an appropriately ordered factor, or (unordered) factor with levels specified in the correct order.

    • ggplot2 respects the order of the factor levels (whether in an ordered factor or not).
    • R will order the levels a factor lexicographically by default, so for any other order, you will need to specify it.

    .

    library(plyr) # for arrange 
    var1_order <- unique(as.character(arrange(test,var2)[['var1']]))
    
    test$var1_order <- ordered(test$var1, levels = var1_order)
    ## or
    test$var1_order_2 <- factor(test$var1, levels = var1_order)
    ## so that 
    ggplot(test, aes(x=var1_order_2, y=value, fill=var2)) + 
     geom_boxplot() + 
     xlab('var1')
    ## or 
    ggplot(test, aes(x=var1_order, y=value, fill=var2)) + 
     geom_boxplot() + 
     xlab('var1')
    

    Will both give the same result

    EDIT — Another approach

    You could have your x axis as var2 and fill by var1, which will order by var2 then dodge and fill by var1

      ggplot(test, aes(x=var2,y=value)) + 
       geom_boxplot(aes(fill = var1))
    

    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 just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.