Bar chart with percentages of the total while plotting multiple charts

Thank you for posting! Here is an outline of an effective post:

Got another challenge while trying to plot multiple bar charts with percentages on the y-axis. When I ran this code with numbers everything worked fine.


ggplot(
data = regions,
mapping = aes(
x = species,
fill = Phenotype)) + scale_fill_manual(values = phenotype_colors) +
geom_bar() +

scale_y_continuous(breaks = seq(from = 0,
to = 10000,
by = 1000),
expand = c(0,0)) +

scale_x_discrete(expand = c(0,0))+
coord_flip()+
theme_classic()+
facet_wrap(~ Regions)+
labs(title = ) +
xlab(“WHO priority pathogens”) + ylab(“Number of isolates”)


But with this code some rows are deleted even though there are no missing data

ggplot(
data = regions2,
mapping = aes(
x = species, y = percentages,
fill = Phenotype)) +
geom_bar(stat=“identity”) +
scale_fill_manual(values = phenotype_colors) +
scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
scale_x_discrete(expand = c(0, 0)) +
coord_flip() +
facet_wrap(~ Regions) +
labs(
title = ,
x = “Species”,
y = “Percentage”)

I want the y axis in percentage and dont want any rows removed because there are no missing data. How do I fix my codes to make this work.

Thanks

1 Like

Hi,

If you remove the line where it says coord_flip() + from your code that will result in the y-axis showing percentages. However, I don’t think any data is missing in your facets, they all share the same x-axis scale (which appears as the y-axis in your plot due to the coordinate flip mentioned above).

All the best,

Tim

Thanks, Tim,

The problem is not solved. Please see below the chart when I used “number of isolates” as opposed to “percentages”. You will observe that some bars are missing in the chart with percentages.

1 Like

Hi,

It looks to me that your percentages are calculated incorrectly but it’s hard to be sure without having access to a reproducible example.

My recommendation would be to review your percentages to make sure that they are valid and to create a reproducible example so that I and others can properly assess the situation.

All the best,

Tim

Hi Tim

Here is the code I used to calculate the percentages

Calculate percentages

regions2 <- regions %>%
  group_by(Regions, species, Phenotype) %>%
  summarise(count = n()) %>%
  ungroup() %>%
  mutate(percentages = count / sum(count) * 100)    ``````

# Convert percentages to numeric if necessary
```` regions2$percentages <- as.numeric(regions2$percentages) ````

Kindly suggest a better way if this is not correct,

Thanks

Mabel
1 Like

Hi Mabel,

In the future it would be beneficial if you provided fake data that we can use to replicate your problem, this is part of a reproducible example.

Without knowing the full details of what you are trying to do, I think your percentages should be calculated in the following way:

regions %>%
  group_by(Regions, species, Phenotype) %>%
  summarise(count = n(), .groups = "drop_last") %>%
  mutate(percentages = (count / sum(count)) * 100) %>%
  ungroup()

All the best,

Tim