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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:10:06+00:00 2026-06-09T10:10:06+00:00

I’m having troubles creating a stacked barchart with aggregate data. When dealing with aggregate

  • 0

I’m having troubles creating a stacked barchart with aggregate data. When dealing with aggregate tables from other people’s reports I generally use Excel, but I’d like to start doing all my charts in R, possibly with lattice or ggplot. In Excel doing a stacked barchart of the following aggregate data takes a couple of clicks (Insert, Column Charts, Stacked Column), and you get something like this.enter image description here.

Besides wanting to this chart in R I also want to use ggplot’s faceting,i.e. put two stacked barcharts side by side in ggplot to compare two groups (A and B).I’ve played around with other charts and this seems the best choice. This is the data.The Excel chart only shows group A (the numbers are percentages).

D<-as.data.frame(structure(list(Group = c("A", "A", "A", "A", "A", 
"A", "B", "B", "B", "B", "B", "B"
), Education = c("NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", 
"Below NVQ Level 2", "Other qualification", "No qualification", 
"NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", "Below NVQ Level 2", 
"Other qualification", "No qualification"), Full.Time = c(47, 
27, 23, 17, 18, 9, 36, 26, 22, 22, 27, 12), PT.16.hours = c(20, 
24, 22, 18, 18, 12, 22, 21, 21, 22, 14, 10), PT.16.hours.1 = c(12, 
11, 10, 11, 13, 5, 24, 25, 25, 20, 16, 12)), .Names = c("Group", 
"Education", "Full.Time", "PT>16.hours", "PT<16.hours")))

Before getting to the faceting to show the difference in the two groups, I’m actually having troubles creating a single stacked bar chart (like the one above) with ggplot2. I’m guessing I shouldn’t have 3 variables (FullTime,PT,PT>16 hours), but rather single rows for each case, so instead of having

A    NVQ Level 4 and above      47  20  12
A    NVQ Level3                 27  24  11

I should have

Group          Education    Work     Percentage
A   NVQ Level 4 and above   Full Time   47
A   NVQ Level 4 and above   PT>16 hours 20

If this is the only way to get ggplot to do the chart, how would you change from one format to the other with a few lines of code?I often find this type of data so it would be good to have a standardised procedure. I’ve also played around with the ggplot option ‘identity’ but haven’t had much success.

Any help would be much appreciated.

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-06-09T10:10:08+00:00Added an answer on June 9, 2026 at 10:10 am

    reshape your data:

    library(reshape2)
    df <- melt(D)
    

    And simply plot it 🙂

    ggplot(df, aes(x = factor(Education), y = value, fill = factor(variable))) +
    geom_bar() + facet_grid(.~Group) +
    ylab('') + xlab('') + opts(title = '') + scale_fill_discrete('') +
    theme_bw() +
    opts(axis.text.x=theme_text(angle = 45, hjust = 1, vjust = 1))
    

    Where the first line creates sets aesthetics, second line adds bar layer and the facet, on the 3rd line we remove unwanted texts from the plot, the 4th line sets the b&w theme and on the last line we rotate the x asis labels.

    enter image description here

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I

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.