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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:27:24+00:00 2026-05-29T15:27:24+00:00

I have a data set which is similar to df <- data.frame(cbind( c(rep.int(x =

  • 0

I have a data set which is similar to

df <- data.frame(cbind(
  c(rep.int(x = 0, times =7), 1:3), 
  c(1, 1, 1, 0, 1, 0, 1, 1, 0, 0),
  c(1:3, 1:3, 1:3, NA)))
names(df) <- c("cars", "sex", "status")
df$sex <- factor(df$sex, labels = c("male", "female"))
df$status <- factor(df$status, labels = c("bad", "ok", "good"))
df$car <- (df$cars > 0) # Person has at least 1 car

I would like to use ggplot2 to make a faceted bar chart with the following characteristics:

  • Facet by the categorical variables (sex and status in this example)
  • Each panel contains one bar per level of that factor (e.g. male and female for “sex”)
  • Each bar shows how many percent of the total observations for that level of that factor, that has at least 1 car (e.g. percent of males with at least 1 car)

How can I do this smoothly in ggplot2? (Or alternatively, do you have a better suggestion of how to represent these proportions graphically?)

  • 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-29T15:27:24+00:00Added an answer on May 29, 2026 at 3:27 pm
    library(ggplot2)
    
    df.long = melt(df, measure.vars=c('sex', 'status'))
    df.long.summary = ddply(df.long, .(variable, value), summarize, cars=sum(cars > 0) / length(cars))
    
    ggplot(data=df.long.summary, aes(x=value, y=cars)) +
      geom_bar(stat='identity') +
      facet_wrap(~variable, scales='free_x') +
      scale_y_continuous(formatter='percent')
    

    enter image description here

    (BTW it’s even a bit simpler in the next version of ggplot2, as there will be no need to compute the summary manually because you can automatically limit the plot range to the summary instead of the raw data)

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

Sidebar

Related Questions

I have a data set which consists of an ID and a matrix (n
I have a set of data points in 3D space which apparently all fall
I have a PHP-generated page which is displaying some data about a set of
I have a set of XSDs from which I generate data access classes, stored
I have a mysql table field set as time type which stores data in
I have a set of controls bound to data, on which I would like
I have several objects set up in Core Data, one of which is Deck
I have a set of data in which I need to code values of
I have a data set (currently in Access 2007 format) which has 2 tables,
I have used KD-tree(libkdtree++) to store a multi-dimensional data set, and the requirements here

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.