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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:48:15+00:00 2026-05-27T01:48:15+00:00

My colleague and I are trying to order a stacked bar graph based on

  • 0

My colleague and I are trying to order a stacked bar graph based on the y-values instead of alphabetically by the x-values.

The sample data is:

library(ggplot2)
samp.data <- structure(list(fullname = c("LJ", "PR", 
                                         "JB", "AA", "NS", 
                                         "MJ", "FT", "DA", "DR", 
                                         "AB", "BA", "RJ", "BA2", 
                                         "AR", "GG", "RA", "DK", 
                                         "DA2", "BJ2", "BK", "HN", 
                                         "WA2", "AE2", "JJ2"), I = c(2L, 
                                                                     1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 
                                                                     3L, 3L, 3L, 3L, 3L, 3L, 3L), S = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                                                                                        2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 
                                                                                                        3L), D = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                   2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L), C = c(0L, 2L, 1L, 
                                                                                                                                                                      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
                                                                                                                                                                      2L, 3L, 3L, 3L, 3L)), .Names = c("fullname", "I", "S", "D", "C"
                                                                                                                                                                      ), class = "data.frame", row.names = c(NA, 24L))
md <- reshape2::melt(samp.data, id = (c("fullname")))
ggplot(data = md, aes(x = fullname, y = value, fill = variable)) +
  geom_col()

But I ultimately want to sort by the sum of the 4 variables (I, S, D, and C) instead of the alphabetical order of the fullnames.

  • 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-27T01:48:16+00:00Added an answer on May 27, 2026 at 1:48 am

    The general (non ggplot-specific) answer is to use reorder() to reset the factor levels in a categorical column, based on some function of the other columns.

    ## Examine the default factor order
    levels(samp.data$fullname)
    
    ## Reorder fullname based on the the sum of the other columns
    samp.data$fullname <- reorder(samp.data$fullname, rowSums(samp.data[-1]))
    
    ## Examine the new factor order
    levels(samp.data$fullname)
    attributes(samp.data$fullname)
    

    Then just replot, using code from the original question

    md <- melt(samp.data, id=(c("fullname")))
    temp.plot<-ggplot(data=md, aes(x=fullname, y=value, fill=variable) ) + 
                   geom_bar()+ 
                   theme(axis.text.x=theme_text(angle=90)) + 
                   labs(title = "Score Distribtion")
    ## ggsave(temp.plot,filename="test.png")
    

    enter image description here

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

Sidebar

Related Questions

a colleague and I are trying pair programming for the first time. We both
I'm working with a colleague and we're trying to reproduce the following SQL Query
Using google docs, a colleague has created a plot similar to: I'm trying to
Me and my colleague are trying to implement a mechanism to get recovery from
I'm trying to convince a colleague that a function should take an interface as
I'm trying to help a colleague with some code in a client side extension.
I'm trying to fill in for a colleague in doing some Oracle work, and
A colleague is trying to import a Java web project project into his Eclipse
I'm working with a colleague new to programming and I'm trying to explain some
Here's the thing. A colleague of mine is trying to overwrite the session handling

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.