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

Johanna

I'm looking for a way to label a stacked bar chart with percentages while the y-axis shows the original count (using ggplot). Here is a MWE for the plot without labels:

library(ggplot2)
df <- as.data.frame(matrix(nrow = 7, ncol= 3,
                       data = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7",
                                "north", "north", "north", "north", "south", "south", "south",
                                "A", "B", "B", "C", "A", "A", "C"),
                      byrow = FALSE))

colnames(df) <- c("ID", "region", "species")

p <- ggplot(df, aes(x = region, fill = species))
p  + geom_bar()

I have a much larger table and R counts quite nicely the different species for every region. Now, I would like to show both, the original count value (preferably on the y-axis) and the percentage (as label) to compare proportions of species between regions.

I tried out many things using geom_text() but I think the main difference to other questions (e.g. this one) is that

  • I do not have a separate column for y values (they are just the counts of different species per region) and
  • I need the labels per region to sum up to 100% (since they are considered to represent seperate populations), not all labels of the entire plot.

Any help is much appreciated!!

eipi10

As @Gregor mentioned, summarize the data separately and then feed the data summary to ggplot. In the code below, we use dplyr to create the summary on the fly:

library(dplyr)

ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
         mutate(pct=n/sum(n),               # Calculate percent within each region
                ypos = cumsum(n) - 0.5*n),  # Calculate label positions
       aes(region, n, fill=species)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%"), y=ypos))

enter image description here

Update: With dplyr 0.5 and later, you no longer need to provide a y-value to center the text within each bar. Instead you can use position_stack(vjust=0.5):

ggplot(df %>% count(region, species) %>%    # Group by region and species, then count number in each group
         mutate(pct=n/sum(n)),              # Calculate percent within each region
       aes(region, n, fill=species)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%")), 
            position=position_stack(vjust=0.5))

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

Add percentage labels to stacked bar chart ggplot2

Stacked Bar Chart by Units & Percentage

R ggplot labels on stacked bar chart

R ggplot2 stacked barplot, percent on y axis, counts in bars

Manually change order of y axis items on complicated stacked bar chart in ggplot2

Percentage stacked bar chart pandas

Calculating percentage and making ggplot bar chart in R

Special Stacked Bar Chart R ggplot

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

Change histogram bar percentage label in R ggplot

Binary column sums into percentage stacked bar chart in 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?

Grouped stacked bar chart in ggplot2 where each stack corresponds to its y axis value

Stacked bar chart swap x-axis and y-axis

Overlayed Stacked Percentage Bar Chart

How to control the spacings of the x Axis label positions in a stacked bar chart?

How to show value label in stacked and grouped bar chart using ggplot

R ggplot Sort Percent Stacked Bar Chart

plotting a percentage stacked bar chart in R

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

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

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

Create Stacked "Progress" Bar Chart in R with ggplot

How to create a stacked bar chart in r with ggplot

Label column in stacked bar chart ggplot2

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

Y axis in single stacked bar chart