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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:23:04+00:00 2026-06-13T06:23:04+00:00

I am trying to create a faceted barplot, with percentages adding up to 100

  • 0

I am trying to create a faceted barplot, with percentages adding up to 100 for each facet. The solution to this seems to be a combination of group and ..density... How ever – it seems to me that groupis conflicting with fill.

Data:

test <- data.frame(
     test1 = sample(letters[1:2], 100, replace = TRUE), 
     test2 = sample(letters[3:8], 100, replace = TRUE)
 )

This gets the percentages right:

ggplot(test, aes(test2)) +
     geom_bar(aes(y = ..density.., fill=test2,group=test1)) + 
     facet_grid(~test1)

Bus as you can see, fillis overwritten:
percentage sums to 100 for each facet

However, the code below respects fill but gives me the wrong percentages (sums to 100 for the whole chart)(using ..density..):

ggplot(test, aes(test2)) +
     geom_bar(aes(y = ..count../sum(..count..), fill=test2)) + 
     facet_grid(~test1)

percentage sums to 100 for the total chart

Related: This old question of mine: percentage on y lab in a faceted ggplot barchart?.

And yes – I could create additional data, but I feel this belongs in the presentation layer. Actually this feels like a bug?

  • 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-13T06:23:05+00:00Added an answer on June 13, 2026 at 6:23 am

    This is a bit of a hack, but you can reference ..x.. within the geom_bar call. The only problem is that ggplot considers this numeric and so I have coerced to factor and given nice labels within a call to scale_fill_brewer

    ggplot(test, aes(x= test2, group = test1)) + 
       geom_bar(aes(y = ..density.., fill = factor(..x..))) + 
       facet_grid(~test1) + 
       scale_fill_brewer(name = 'test2', breaks = 1:6, 
                         labels = levels(test$test2), palette = 'Set3')
    

    enter image description here

    compare with not coercing ..x.. to a factor

    ggplot(test, aes(x= test2, group = test1)) + 
      geom_bar(aes(y = ..density.., fill = ..x..)) +
       facet_grid(~test1)
    

    enter image description here

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

Sidebar

Related Questions

Trying to create a background-image slideshow and am getting this error... This is the
I'm trying create a log specifically for logging user account activity. I created this
Trying to create the following routine in MySQL Workbench yields a This object's DDL
I'm trying create this function such that if any key besides any of the
I am trying create a table and add this table into tablecell (table inside
I'm trying create a small http proxy service. This is not working so well.
I trying create empty white image with php, this s my code $bg =
I am trying create my custom Style spinner with help of this site. But
I'm trying create simple application in C++. This application has to read from file
I'm trying create a simple background periodic task using Django-Celery-RabbitMQ combination. I installed Django

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.