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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:34:15+00:00 2026-05-23T20:34:15+00:00

I have some problems with making a stacked bar chart in ggplot2. I know

  • 0

I have some problems with making a stacked bar chart in ggplot2. I know how to make one with barplot(), but I wanted to use ggplot2 because it’s very easy to make the bars have the same height (with ‘position = ‘fill”, if I’m not mistaken).

My problem is that I have multiple variables that I want to plot on top of each other; my data looks like this:

dfr <- data.frame(
  V1 = c(0.1, 0.2, 0.3),
  V2 = c(0.2, 0.3, 0.2),
  V3 = c(0.3, 0.6, 0.5),
  V4 = c(0.5, 0.1, 0.7),
  row.names = LETTERS[1:3]
)

What I want is a plot with categories A, B, and C on the X axis, and for each of those, the values for V1, V2, V3, and V4 stacked on top of each other on the Y axis. Most graphs that I have seen plot only one variable on the Y axis, but I’m sure that one could do this somehow.

How could I do this with ggplot2? 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-23T20:34:16+00:00Added an answer on May 23, 2026 at 8:34 pm

    First, some data manipulation. Add the category as a variable and melt the data to long format.

    dfr$category <- row.names(dfr)
    mdfr <- melt(dfr, id.vars = "category")
    

    Now plot, using the variable named variable to determine the fill colour of each bar.

    library(scales)
    (p <- ggplot(mdfr, aes(category, value, fill = variable)) +
        geom_bar(position = "fill", stat = "identity") +
        scale_y_continuous(labels = percent)
    )
    

    (EDIT: Code updated to use scales packages, as required since ggplot2 v0.9.)

    enter image description here

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

Sidebar

Related Questions

i want to use .animate function but i have some problems i want to
I'm able to make Facebook apps. But I have some problems. The documentation highlights
I am just starting Java RMI and have some problems with when to use
I am making a bread crumb bar in Delphi and having some problems regarding
I'm making an ajax request and I have some problems, this is my jquery
I'm starting with linqToSql and I still have some problems making my queries. For
I have some problems with making my web-app adapted for remote user browseres language
I have seen couple of similar problems with solutions, but I couldn't find one
I have some problems making a google map that loads XML file that have
I'm currently making a webpage and I have run in to some problems with

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.