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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:49:34+00:00 2026-06-18T05:49:34+00:00

First, a quick example to set the stage: set.seed(123) dat <- data.frame( x=rep( c(1,

  • 0

First, a quick example to set the stage:

set.seed(123)
dat <- data.frame( 
  x=rep( c(1, 2, 4, 7), times=25 ), 
  y=rnorm(100), 
  gp=rep(1:2, each=50) 
)

p <- ggplot(dat, aes(x=factor(x), y=y))
p + geom_boxplot(aes(fill = factor(gp)))

I would like to produce a similar plot, except with control over the x position of each set of boxplots. My first guess was using a non-factor x aesthetic that controls the position along the x-axis of these box plots. However, once I try to do this it seems like geom_boxplot doesn’t interpret the aesthetics as I would hope.

p + geom_boxplot( aes(x=x, y=y, fill=factor(gp)) )

In particular, geom_boxplot seems to collapse over all x values in some way when they’re non-factors.

Is there a way to control the x position of boxplots with ggplot2? Either through specifying a distance between each level of a factor aesthetic, some more clever use of non-factor aesthetics, or otherwise?

  • 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-18T05:49:36+00:00Added an answer on June 18, 2026 at 5:49 am

    You can use scale_x_discrete() to set positions (ticks) for the x axis.

    p <- ggplot(dat, aes(x=factor(x), y=y))
    p + geom_boxplot(aes(fill = factor(gp))) + 
        scale_x_discrete(limits=1:7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First a quick definition :) Template - A string which may contain placeholders (example:hello
Quick summary of my problem: first rendering of the view contains the elements in
First time poster here. A quick question about setting up a loop here. I
Quick question, indexof() find the first occur position of the string character? what about
this is my first question on stack overflow. Some quick background, this is not
This is my first usage of jQuery. I just want a quick solution for
I have a quick question here. I know that the cakePHP find('first') function returns
I recently used ActiveAdmin for the first time to toss together a very quick
first, an example: given a bunch of Person objects with various attributes (name, ssn,
Lets say I have the following xml (a quick example) <rows> <row> <name>one</name> </row>

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.