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

Iñaki Baglivo

I have a dataset that has the variables "SEXO" (M or F) and "Class" (0 or 1). I want to create a bar plot using ggplot2 that shows, for each sex, the distribution of Class as a percentage. I was able to get the plot, but I can't seem to get the labels working on the bars itself. I don't want to change the labels on the axis, I just want to get the % shown on the plot for each SEXO.

This is the code I have been using:

ggplot(data = df, aes(x = SEXO, fill = Class)) + geom_bar(position = 'fill')

I also attach an image of the plot produced by the code: enter image description here

This would be the ideal outcome: enter image description here

Quinten

Here an example using the mtcars dataset where you can calculate the percentage per group and use these to place in your bars using label with geom_text like this:

library(ggplot2)
library(dplyr)
mtcars %>%
  group_by(am, vs) %>%
  summarise(cnt = n()) %>%
  mutate(perc = round(cnt/sum(cnt), 2)) %>%
  ggplot(aes(x = factor(vs), fill = factor(am), y = perc)) + 
  geom_col(position = 'fill') +
  geom_text(aes(label = paste0(perc*100,"%"), y = perc), position = position_stack(vjust = 0.5), size = 3) +
  labs(fill = 'Class', x = 'vs') +
  scale_y_continuous(limits = c(0,1))
#> `summarise()` has grouped output by 'am'. You can override using the `.groups`
#> argument.

Created on 2022-11-02 with reprex v2.0.2

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

R ggplot labels on stacked bar chart

Add percentage labels to stacked bar chart ggplot2

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

R, ggplot stacked bar-chart with position = "fill" and labels

Creating percentage stacked bar chart using groupby

creating stacked bar chart in r

plotting a percentage stacked bar chart in R

R plotly show only labels in the stacked bar chart where percentage value for the stack is above 5

Add percentage labels to a stacked bar chart above bars

R stacked percentage bar plot with percentage of binary factor and labels

Stacked bar chart showing labels in reverse in R

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

Binary column sums into percentage stacked bar chart in ggplot

R Stacked percentage bar plot with percentage of two factor variables with ggplot

Stacked clustered percentage bar with labels

Special Stacked Bar Chart R ggplot

Create Stacked "Progress" Bar Chart in R with ggplot

R ggplot Sort Percent Stacked Bar Chart

How to create a stacked bar chart in r with ggplot

Absolute labels for proportional stacked bar chart in ggplot2

ggplot2 put labels on a stacked bar chart

ggplot2 stacked bar chart labels with leader lines

Stacked Bar Chart by Units & Percentage

Percentage stacked bar chart pandas

Overlayed Stacked Percentage Bar Chart

Calculating percentage and making ggplot bar chart in R

R code - help creating a 100% stacked bar chart like the below image with ggplot:

How do I create a frequency stacked bar chart however have percentage labels on the bars and frequencies on the y axis, in R?