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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:08:55+00:00 2026-06-16T15:08:55+00:00

I am trying to create a stacked bar chart with multiple variables, but I

  • 0

I am trying to create a stacked bar chart with multiple variables, but I am stuck on two issues:

1) I can’t seem to get the rotated y-axis to display percentages instead of counts.

2) I would like to sort the variables (desc) based on the percentage of the “strongly agree” response.

Here is an example of what I have so far:

require(scales)
require(ggplot2)
require(reshape2)

# create data frame
  my.df <- data.frame(replicate(10, sample(1:4, 200, rep=TRUE)))
  my.df$id <- seq(1, 200, by = 1)

# melt
  melted <- melt(my.df, id.vars="id")

# factors
  melted$value <- factor(melted$value, 
                         levels=c(1,2,3,4),
                         labels=c("strongly disagree", 
                                  "disagree", 
                                  "agree", 
                                  "strongly agree"))
# plot
  ggplot(melted) + 
    geom_bar(aes(variable, fill=value, position="fill")) +
    scale_fill_manual(name="Responses",
                      values=c("#EFF3FF", "#BDD7E7", "#6BAED6",
                               "#2171B5"),
                      breaks=c("strongly disagree", 
                               "disagree", 
                               "agree", 
                               "strongly agree"),
                      labels=c("strongly disagree", 
                               "disagree", 
                               "agree", 
                               "strongly agree")) +
    labs(x="Items", y="Percentage (%)", title="my title") +
    coord_flip()

I owe thanks to several folks for help in getting this far. Here are a few of the many pages that Google served up:

http://www.r-bloggers.com/fumblings-with-ranked-likert-scale-data-in-r/

Create stacked barplot where each stack is scaled to sum to 100%

sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reshape2_1.2.2  ggplot2_0.9.2.1 scales_0.2.2   

loaded via a namespace (and not attached):
 [1] colorspace_1.2-0    dichromat_1.2-4     digest_0.6.0        grid_2.15.0         gtable_0.1.1        HH_2.3-23          
 [7] labeling_0.1        lattice_0.20-10     latticeExtra_0.6-24 MASS_7.3-22         memoise_0.1         munsell_0.4        
[13] plyr_1.7.1          proto_0.3-9.2       RColorBrewer_1.0-5  rstudio_0.97.237    stringr_0.6.1       tools_2.15.0       
  • 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-16T15:08:57+00:00Added an answer on June 16, 2026 at 3:08 pm

    For (1)
    To get percentages, you’ll have to create a data.frame from melted. At least that’s the way I could think of.

    # 200 is the total sum always. Using that to get the percentage
    require(plyr)
    df <- ddply(melted, .(variable, value), function(x) length(x$value)/200 * 100)
    

    Then supply the calculated percentages as weights in geom_bar as follows:

    ggplot(df) + 
    geom_bar(aes(variable, fill=value, weight=V1, position="fill")) +
    scale_fill_manual(name="Responses",
                      values=c("#EFF3FF", "#BDD7E7", "#6BAED6",
                               "#2171B5"),
                      breaks=c("strongly disagree", 
                               "disagree", 
                               "agree", 
                               "strongly agree"),
                      labels=c("strongly disagree", 
                               "disagree", 
                               "agree", 
                               "strongly agree")) +
    labs(x="Items", y="Percentage (%)", title="my title") +
    coord_flip()
    

    I don’t quite understand (2). Do you want to (a) calculate relative percentages (with reference as “strongly agree”? Or (b) do you want always the plot to display “strongly agree”, then “agree”, etc.. You can accomplish (b) by just reordering factors in df by,

    df$value <- factor(df$value, levels=c("strongly agree", "agree", "disagree", 
                     "strongly disagree"), ordered = TRUE)
    

    Edit: You can reorder the levels of variable and value to the order you require as follows:

    variable.order <- names(sort(daply(df, .(variable), 
                         function(x) x$V1[x$value == "strongly agree"] ), 
                         decreasing = TRUE))
    value.order <- c("strongly agree", "agree", "disagree", "strongly disagree")
    df$variable <- factor(df$variable, levels = variable.order, ordered = TRUE)
    df$value <- factor(df$value, levels = value.order, ordered = TRUE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a stacked bar chart , But I'm getting an error
I am trying to create a Stacked Bar Chart. My requirement is I need
I'm trying to create a stacked bar chart with d3 from data in a
I am trying to use nvd3 to create a vertical stacked bar chart. I
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I am trying to create horizontal bar chart to look like a pill chart.
I am trying to create a stacked column chart in VBA where there is
Thanks in advance for your response. I am trying to create a stacked bar
I'm trying to create a relatively complex MySQL query to generate a stacked bar
What I'm trying to do is create a 2d stacked chart where the position

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.