how to change the stacked bar chart using ggplot2 (percentage, sort) in R

JimmyBeam

I am new to R and ggplot2. I have a dataset that I want to visualize with stacked bar chart, where

  • x axis is categorical variable (sex)
  • bar consists of categorical variable (day)
  • y axis should be percentile.

Example..

total_bill tip sex smoker day time size
16.99 1.01 Female No Sun Dinner 2
10.34 1.66 Male No Sun Dinner 3
    data(tips, package='reshape2')
    
    ggplot(tips, aes(x=sex)) +
    geom_bar(aes(fill=day), width = 0.5, position = 'fill')+
    theme(axis.text.x = element_text(angle=65, vjust=0.6))

from here, I want to make the following changes in the chart.

  1. to take out 'Fri' from the chart, but the percentage of the other days should remain same. This means the percentage of 'Sat, Sun, Thur' is not re-mapped to 100%
  2. to sort the 'sex' with descending order of 'Sat' percentage. This means, if 'Male' has higher percentage of 'Sat' than the one of 'Female', 'Male' should come on the left.
Vinícius Félix
library(tidyverse)

data(tips, package='reshape2')

tips %>% 
  #Calculating percentage by sex outside ggplot2
  count(sex,day) %>% 
  group_by(sex) %>% 
  mutate(p = 100*n/sum(n)) %>% 
  ungroup() %>%
  #Removing Friday 
  filter(day != "Fri") %>% 
  #Ordering sex by Saturday percentage
  mutate(
    sex = fct_reorder2(
    .f = sex,
    .x = p,
    .y = day,
    .fun = function(x,y) max(x[y == "Sat"])
      )
    ) %>% 
  ggplot(aes(x = sex, y = p)) +
  geom_col(aes(fill=day))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Creating a Stacked Percentage Bar Chart in R with ggplot

How to change the position of stacked stacked bar chart in ggplot in R?

Stacked Bar Chart in R using ggplot2 (not possible in excel)

How to created a grouped and stacked bar chart in R with ggplot2

R ggplot Sort Percent Stacked Bar Chart

Add percentage labels to stacked bar chart ggplot2

How to organize percentage values on top of a stacked bar chart ggplot2

R: ggplot stacked bar chart with counts on y axis but percentage as label

Creating a Stacked Percentage Bar Chart in R with ggplot with labels

ggplot2 group and sort for stacked bar chart

Change the stacked bar chart to Stacked Percentage Bar Plot

How to plot Bar_Chart of accuracy scores as percentage value for multiple variables and subgroups using ggplot2 R?

plotting a percentage stacked bar chart in R

Creating percentage stacked bar chart using groupby

How to create a stacked bar chart in r with ggplot

R Create stacked bar chart from non-numerical data using ggplot2

How to create percentage stacked bar chart in plotly?

Set border in a stacked bar chart using ggplot2

How to control ordering of stacked bar chart using identity on ggplot2

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

how to make single stacked bar chart in ggplot2

How modify stacked bar chart in ggplot2 so it is diverging

How to change the order and color scheme of stacked bar charts using ggplot2?

ggplot2 stacked bar chart: each bar is one colour, stacked groups are shown using alpha

Plotting stacked bar chart in ggplot2: presenting a variable as percentage of another variable

R Stacked Bar Chart: How to change the order of geom text labels

How to change stacking order in stacked bar chart in R?

How to change the x-axis ticks in a facet-wrapt stacked bar chart--ggplot R

Rows As Stacked Bar Plot Using ggplot2 In R