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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:55:02+00:00 2026-05-30T19:55:02+00:00

I am creating stacked bar charts on subsets of data using a loop. I’m

  • 0

I am creating stacked bar charts on subsets of data using a loop. I’m exporting one stacked bar chart for each variable for each school. Its all working successfully – each chart shows only the subset of data – except that the x axis continues to be labeled for every school. So I have a big long chart area with blanks, and a stacked bar chart over the school for which the data is subsetted.

Here is sample of my data:

label    variable    class    percent
SchoolA   Optimism    High     67
SchoolA   Optimism    Med      33
SchoolA   Optimism    Low      20
SchoolA   SelfEsteem  High     84
SchoolA   SelfEsteem  Med      12
SchoolA   SelfEsteem  Low      4
SchoolB   Optimism    High     60
SchoolB   Optimism    Med      21
SchoolB   Optimism    Low      19
SchoolB   SelfEsteem  High     20
SchoolB   SelfEsteem  Med      42
SchoolB   SelfEsteem  Low      38

…which carries on for several hundred more variables and schools.

In general I’m doing this:

Create an array of values for each school and variable

schools<-unique(df1$label)  
variables<-unique(df1$variable)

Function that plots each subset of data as a stacked bar chart

doPlot<-function(subdf){  
    ggplot(subdf,aes(x=label,y=percent,fill=factor(class)))+  
    geom_bar(stat="identity")}

Runs doPlot using each subset of data

for(i in 1:length(schools)){  
    for (j in 1:length(variables)){  
        subdf<-data.frame(subset(df1,label==schools[i]&variable==variables[j]))  
        doPlot(subdf)}}

Why is the chart creating and labeling an x-axis for the original data set and not only for the subset of data? I am a R newbie, but I’ve been searching and trying things out for a while and I’m stumped.

  • 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-30T19:55:04+00:00Added an answer on May 30, 2026 at 7:55 pm

    It’s because when you subset a dataframe with a factor column (df1$label is a factor with levels ‘SchoolA’ and ‘SchoolB’), the label column still has multiple levels, even though there is currently only one unique label in it.

    e.g.:

    df2 <- subset(df1,label=='SchoolA'&variable=='Optimism')
    df2$label
    # [1] SchoolA SchoolA SchoolA
    # Levels: SchoolA SchoolB
    

    See how even though df2$label is ‘SchoolA’ only, there are still two levels? ggplot uses the levels to draw the graphs.

    ?subset mentions :

    Factors may have empty levels after subsetting; unused levels are
    not automatically removed. See ‘droplevels’ for a way to drop all
    unused levels from a data frame.

    Then going to ?droplevels: “used to drop unused levels from .. factors in a data frame”.

    So try doing:

    subdf <- droplevels(data.frame(subset(df1,label==schools[i]&variable==variables[j])))
    

    Then you’ll get just one variable on the x axis, the current school.

    (As an aside – is a stacked bar chart the best way to represent these? In particular, I notice that SchoolA’s ‘Optimism’ adds up to 120% overall, and the stacked bar chart seems to enforce this understanding of the data. If that’s not how you want the data to be interpreted, perhaps you could consider a different way of presenting it?)

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

Sidebar

Related Questions

I have stumbled accross the following snippet, for creating horizontal bar chart using matplotlib:
Creating My Windows Form Application and using ADO.Net as Data Access layer and SQL
I'm doing this system Stacked and I am creating the search function. And in
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
I am creating a site with lots of big scrollable text-boxes in it. Each
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating sprite sheets aka texture atlases rather than using many hundres of individual images
Creating the closure is easy but using it is confusing for me. Here is
creating 20 viewcontrollers and calling them one after the other from the mainviewcontroller as
Creating a zip file using Send To -> Compressed folder excludes .hg folder on

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.