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

Jed

I started with the code below, however it is not showing the right output. I would just like a normal frequency stacked bar chart to show percentages on the bars but frequencies on the y axis. Could anyone offer any suggestions please?

ggplot(data = df, mapping = aes(x = Family_Size, y = Freq, fill = Survived)) + geom_bar(stat = "identity") + geom_text(aes(label = paste0(df$Percentage),y=Percentage),size = 3) + theme(plot.title = element_text(hjust = 0.5))

<table><tbody><tr><th>Survived</th><th>Family_Size</th><th>Frequency</th><th>Percentage</th></tr><tr><td>Yes</td><td>1</td><td>20</td><td>20%</td></tr><tr><td>No</td><td>1</td><td>80</td><td>80%</td></tr><tr><td>Yes</td><td>2</td><td>40</td><td>40%</td></tr><tr><td>No</td><td>2</td><td>60</td><td>60%</td></tr></tbody></table>

dc37

Are you looking for something like that ?

ggplot(df, aes(x = Family_Size, y = Frequency, fill = Survived))+
  geom_col()+
  scale_y_continuous(breaks = seq(0,100, by = 20))+
  geom_text(aes(label = Percentage), position = position_stack(0.5))

enter image description here


EDIT: Formatting percentages with two decimales

ggplot(df, aes(x = Family_Size, y = Frequency, fill = Survived))+
  geom_col()+
  scale_y_continuous(breaks = seq(0,100, by = 20))+
  geom_text(aes(label = paste(format(round(Frequency,2),nsmall = 2),"%")), position = position_stack(0.5))

enter image description here


Reproducible example

structure(list(Survived = c("Yes", "No", "Yes", "No"), Family_Size = c(1L, 
1L, 2L, 2L), Frequency = c(20L, 80L, 40L, 60L), Percentage = c("20%", 
"80%", "40%", "60%")), row.names = c(NA, -4L), class = c("data.table", 
"data.frame"))

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 stacked bar chart with counts on y axis but percentage as label

Stacked bar chart bars dimension is not coralated to Y axis

How do I position bar chart labels at the "base" of bars

How To Create Pyramid Bar Chart in R with y-axis labels between the bars

Add percentage labels to a stacked bar chart above bars

How do I create stacked barplots / catplots with ambigious Y axis?

How do I plot a stacked bar chart with multiple values for a single entry? Frequency chart

Stacked Bar Chart: incorrect height of bars and labels

How can I create a horizontal stacked bar chart with labels on the bars themselves as well as labels above the bars?

How do I add values to the bars in a Stacked Bar chart in Pygal?

In Chart.js, how do I hide certain axis labels from a stacked bar chart?

How to have percentage labels on a "bar chart of counts"

plotting a percentage stacked bar chart in R

How do I create error bars on a grouped bar plot in R?

How do you create a stacked percentage bar chart in R Shiny app with two reactive values?

How to place data labels on stacked bar chart with categorical variables in R?

How to create a stacked bar chart in r with ggplot

How can I make a faceted bar chart use stacked bars?

How to add percentage labels in a stacked bar keeping bars heights (R plotly)

How to create percentage stacked bar chart in plotly?

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

LWC Chart js: How to display dataset labels on each of the bars below in single horizontal stacked bar chart js

How to label values inside stacked bar graph in R when the y-axis is in percentage

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

How do I make a stacked bar graph in Vega-Lite where the bars are count but the data labels are percent?

How do I create a stacked bar chart with summary data?

Y axis in single stacked bar chart

How can I add 2 100% stacked bars (Y Axis) to each element in the X Axis of my chart in plotly?