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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:48:22+00:00 2026-05-30T21:48:22+00:00

I have data in a csv file in the following format ,BC1,BC10,BC11 1,2432,420,18 2,276,405,56

  • 0

I have data in a csv file in the following format

,BC1,BC10,BC11
1,2432,420,18
2,276,405,56
3,119,189,110
4,90,163,140
5,206,280,200
6,1389,1080,1075
7,3983,3258,4878
8,7123,15828,28111
9,8608,48721,52576
10,9639,44725,55951
11,8323,45695,32166
12,2496,18254,26600
13,1524,8591,18583
14,7861,1857,1680
15,10269,5165,4618
16,13560,64636,63262

I acquire the data in the following way

data <- read.csv(file="file.csv",sep=",",header=TRUE)
data <- data[,2:ncol(dat)]

Then transform it

datam <- melt(cbind(data,ind=rownames(data)),is.var = c('bind'))

Then I create a stacked percent plot

ggplot(datam,aes(x = variable, y = value,fill = ind)) + 
geom_bar(position = "fill") + 
scale_y_continuous(labels = percent_format())

To which I get:

enter image description here

I am trying to order the values in the legend that should go (1…16) instead of 1,10,…,16,2…,9. The color order is correct in the legend though. Thanks

  • 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-30T21:48:23+00:00Added an answer on May 30, 2026 at 9:48 pm

    It’s because ind (your fill variable) is being sorted as a string factor.

    You can convert ind to numeric and then use it as the fill argument:

    datam$ind <- as.numeric(datam$ind)
    ggplot(datam, aes(x=variable, y=value, fill=factor(ind))) + ...
    

    However this does give factor(ind) as the title for the legend. You could do:

    datam$ind <- factor(as.numeric(datam$ind))
    ggplot(datam, aes(x=variable, y=value, fill=ind)) + ...
    

    Or, you could just use scale_fill_discrete('My Legend Name') and avoid having to convert datam, e.g.:

    ggplot(datam, aes(x=variable, y=value, fill=factor(as.numeric(ind)))) + ... +
          scale_fill_discrete('legend name')
    

    Edit

    The ordering of the legend labels is determined by levels(datam$ind):

    > levels(datam$ind)
    [1] "1"  "10" "11" "12" "13" "14" "15" "16" "2"  "3"  "4"  "5"  "6"  "7"  "8" 
    [16] "9" 
    

    That’s why you originally got the string-wise order.
    By doing factor(as.numeric(ind)), levels(datam$ind) got re-sorted in numeric order.

    So to specify them going from 16 to 1, say, you can do:

    lvls <- levels(datam$ind) # "1" "10" "11" ...
    lvls <- sort(as.numeric(lvls)) # 1 2 3 4 ..
    lvls <- rev(lvls)              # 16 15 14 13 ...
    # re-level datam$ind according to lvls:
    datam$ind <- factor(datam$ind, levels=lvls)
    
    # now we can plot!
    ggplot(datam, aes(x=variable, y=value, fill=ind)) + ...
    

    Hmm, that did change the colours/stacking order though. I’m unsure how to have the labels go one way but the stacking go the other — ggplot always has them in sync (ie if labels are 1:16, then so is the stacking order). Re switching the colours around, I’m sure there’s some ggplot way to reverse the colour scale, probably via scale_colour_manual (as that’s just a cosmetic change) – I don’t know how to do that myself though.

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

Sidebar

Related Questions

I have a CSV file in the following format: id,code,date,address,complete_url Example data from the
I have data in a csv file in the following format 0,q20,q25,q30 0,32821,2270,68 1,105586,11366,38
I've data in a csv file with the following format : start;duration 1;4 7;3
I have the following code that exports a csv file : //Selecting Item Data
I have a large tab-delimited csv file with the following format: #mirbase_acc mirna_name gene_id
I have to write huge data in text[csv] file. I used BufferedWriter to write
I have some questions about importing data from Excel/CSV File into SQL Server. Let
I have a number of images and a CSV data file that I want
I have a System.Data.DataTable which is populated by reading a CSV file which sets
I have made a java application that stores data from a .csv file to

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.