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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:42:46+00:00 2026-06-19T03:42:46+00:00

I have a dataframe df like this one: Id Var1 Var2 Var3 001 yes

  • 0

I have a dataframe df like this one:

Id    Var1    Var2    Var3
001   yes     no      yes
002   no      no      yes
003   yes     yes     no

I want to create a barplot with 3 bars that represents proportions of yesand nofor Var1 Var2 Var3. Is it possible to do that with ggplotwithout reshaping the dataframe?

Thank you,

corrado

  • 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-19T03:42:47+00:00Added an answer on June 19, 2026 at 3:42 am

    As it was already mentioned it is better to reshape data.

    But if you need to use original data frame then you should use geom_bar() for each column you want to appear on plot. In geom_bar() you should provide x values that will separate those bars – I used variable names as characters. fill= and position="stack" ensures that bars are stacked. y=(..count..)/sum(..count..)) will calculate proportions.

    df <- read.table(text=
    "Id    Var1    Var2    Var3
     001   yes     no      yes
     002   no      no      yes
     003   yes     yes     no",header=T)
    
    library(scales)
    library(ggplot2
    ggplot(df)+
      geom_bar(aes(x="Val1",fill=Var1,y=(..count..)/sum(..count..)),position="stack")+
      geom_bar(aes(x="Val2",fill=Var2,y=(..count..)/sum(..count..)),position="stack")+
      geom_bar(aes(x="Val3",fill=Var3,y=(..count..)/sum(..count..)),position="stack")+
      scale_y_continuous("Percents",labels = percent) + 
      scale_x_discrete("Values")+
      scale_fill_discrete("Legend_title")
    

    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 have a data frame with gaps like this: Var1 Var2 Var3 1 NA
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a input dataframe like this (the real one is very large, so
I have a pandas dataframe object that looks like this: one two three four
I have a dataframe that looks like this: person n start end 1 sam
I'd like to melt the dataframe so that in one column I have dates
I have data like this in R: subjID = c(1,2,3,4) var1 = c(3,8,NA,6) var1.copy
I have a dataframe that looks like this DF: V1 V2 V3 V4 V5
I have a dataframe called ants detailing multiple entries per site, looks like this:

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.