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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:53:54+00:00 2026-05-30T00:53:54+00:00

For a dataframe with three columns — $x — at http://pastebin.com/SGrRUJcA $y — at

  • 0

For a dataframe with three columns —

  • $x — at http://pastebin.com/SGrRUJcA
  • $y — at http://pastebin.com/fhn7A1rj
  • $z — at http://pastebin.com/VmVvdHEE

— I have the following code to generate a dataframe that can be used to plot a stacked bar plot:

counted <- data.frame(table(myDf$x),variable='x')
counted <- rbind(counted,data.frame(table(myDf$y),variable='y'))
counted <- rbind(counted,data.frame(table(myDf$z),variable='z'))

If I then try to sort the dataframe by its Var1 column, as so —

counted.sort <- sort_df(counted,vars="Var1")

— I get a dataframe in which column Var1 now has levels in the following order:

"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", 19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", 36", 37", "39", "42", "46", "47", "53", "54", "38",
"40", "41", "43", "44", "45", "48", "49", "50"

The result distorts the x-axis of my bar plot (…,”54″, “38”, “40”, “41”, “43”,…).

How can I get counted.sort to be sorted correctly by ascending number?

NB. Plotting is done as below:

stackedBp <- ggplot(counted,aes(x=Var1,y=Freq,fill=variable))
stackedBp <- stackedBp+geom_bar(stat='identity')+scale_x_discrete('Levels')+scale_y_continuous('Frequency')
stackedBp
  • 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-30T00:53:55+00:00Added an answer on May 30, 2026 at 12:53 am

    As mentioned in a comment, R’s ?factor is confusing you.

    Let’s start with table. This command is for cross-classifying observations into different, well, categories. Implicit here is that the resulting categories are most often going to be categorical rather than numeric.

    This is why when you pass the table to data.frame the table categories are converted to a factor. Inspect the structure of counted:

    > str(counted)
    'data.frame':   138 obs. of  3 variables:
     $ Var1    : Factor w/ 53 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
     $ Freq    : int  126 711 1059 2079 3070 2716 2745 3329 2916 2671 ...
     $ variable: Factor w/ 3 levels "x","y","z": 1 1 1 1 1 1 1 1 1 1 ...
    

    There’s is an important distinction between the levels and the labels of factors. Internally, factors are always stored as integer codes, and these codes may not correspond to your labels. The internal codes are totally arbitrary, just for bookkeeping.

    The safe way to proceed is to do two coercions:

    counted$Var1 <- as.numeric(as.character(counted$Var1))
    

    and then explicitly convert it back to a factor in your ggplot2 code:

    stackedBp <- ggplot(counted,aes(x=factor(Var1),y=Freq,fill=variable))
    

    Also, you should keep in mind that sorting the data frame does nothing to change the underlying order associated with the factors in that data frame. To alter that behavior you actually need to use the ordered argument to factor when specifying the levels directly. Sorting the data frame simply changes the order in which the values are stored, not how the levels are ordered.

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

Sidebar

Related Questions

I have a dataframe with three columns: Id , Date and Value and want
I have a dataframe with 6 columns and many rows that includes positions for
I have a dataframe with a column of integers that I would like to
Say I have a dataframe df with two or more columns, is there an
I have a dataframe of 9 columns consisting of an inventory of factors. Each
Suppose that you have a data frame with many rows and many columns. The
I have a dataframe (14.5K rows by 15 columns) containing billing data from 2001
I have a dataframe in R with three variables named for example df$V1 ,
I have a dataframe with columns labeled A,B & C. I want to add
I currently have a dataset which has two columns that I'd like to compare.

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.