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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:20:42+00:00 2026-05-21T14:20:42+00:00

I’m fairly new to R, but I’m making good progress. I’ve been able to

  • 0

I’m fairly new to R, but I’m making good progress. I’ve been able to bend ggplot2 to my will with the exception of one thing: the order that the categorical labels are plotted along the x axis in my boxplot. I think this is just a hole in my knowledge of how to address ranges of a dataframe in formulas, but here’s the fake data, as a dataframe called df:

Index    Label    Value
index1   A        1
index2   A        2
index3   A        3
index4   B        12
index5   B        11
index6   B        10
index7   C        8
index8   C        7
index9   C        9
...
index76  Z        15
index77  Z        17
index78  Z        16

My plot code looks like qplot(df$Label, df$Value, data=df) + scale_x_discrete("Label") + opts(axis.text.x = theme_text(angle = 90, hjust = 0, size=7)) + geom_boxplot() and gives me exactly what I want, which is a boxplot showing one box & whiskers for label A, one for B, and one for C. However, the axis goes in the order of the labels (the boxplot of 1,2,3 being closest to the origin, 10,11,12 in the middle, 7,8,9 on the right of the graph). What I want is for the boxplot data to start with the subset that has the highest within label average and proceed in decreasing order. I can average within each label by mean(df$Label[1:3]) and mean(df$Label[4:6]) etc., but I can’t figure out how to get the graph to display such that the plots for the labels go not in the order they appear in factor(df$Label) (i.e. A, B, C along the x with boxes at 2, 11, 8) but in order of highest within-label average to lowest (i.e. B, C, An along the x and the boxes then at 11, 8, 2).

I’m thinking I would create a vector consisting of each within-label average and somehow pass that to ggplot to specify the axis order, but I can’t figure out how to create the vector to start with.

What I need to know is:

What’s the best way to get a vector consisting of the averages of each label, in order from highest to lowest?

How do I pass that vector to ggplot so that it orders the x-axis by those values, while still labeling the x axis with factor(df$Label)

I’m open to suggestions for other ways to display the data as well, but I think I’m pretty close to what I want & the mean & spread of the values within a given label is important.

  • 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-05-21T14:20:43+00:00Added an answer on May 21, 2026 at 2:20 pm

    Here is one way to do it

    # create a dummy data frame
    set.seed(1234)
    df = data.frame(
           label = rep(letters[1:3], each = 3),
           value = sample(100, 9))
    
    # boxplot without sorting
    qplot(label, value, data = df, geom = 'boxplot')
    

    enter image description here

    # boxplot with label sorted by median of value
    qplot(reorder(label, value, median), value, data = df, geom = 'boxplot')
    

    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'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 want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.