Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to group bars together in a stacked bar plot? ggplot R

How do I get the stacked bars in group KAPA, TAKARA and plasmid_DNA to group separately on this bar chart?

This is what I’ve tried, but each group contains all columns even if empty:

s %>%
  rownames_to_column(var = "id") %>%
  filter(id != "miseq_pDNA") %>%
  select(id, unmapped_multihit:mapped_correct) %>% 
  mutate(group = c(rep('KAPA', 4), rep('TAKARA', 4), 'plasmid_DNA')) %>%
  pivot_longer(unmapped_multihit:mapped_correct) %>% 
  ggplot(aes(x = id, y = value, fill = name)) + 
  geom_col(position = position_fill(reverse = T)) +
  scale_y_continuous(labels = scales::percent) +
  facet_wrap(~ group)

This is my data
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> dput(s)
structure(list(total_pairs = c(35894878L, 26257107L, 32170406L, 
17432039L, 46844537L, 29392398L, 41038453L, 33939207L, 24109293L, 
17881958L), pc_aberrant_match = c(911L, 656L, 775L, 1102L, 774L, 
2652L, 5217L, 213L, 128L, 80L), pc_ambiguous = c(1146L, 88L, 
3L, 952L, 115L, 9L, 1829L, 217L, 23L, 21L), pc_f_multi_3p = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), pc_f_multi_5p = c(59948L, 
47425L, 55476L, 24988L, 65678L, 36836L, 58160L, 55139L, 3517L, 
2803L), pc_f_open_3p = c(1930323L, 1471085L, 1761019L, 948152L, 
3718951L, 2282510L, 3143527L, 2399348L, 1205911L, 943323L), pc_f_open_5p = c(1031640L, 
751738L, 940874L, 504689L, 2628015L, 1615047L, 2172979L, 1560282L, 
604596L, 338024L), pc_match = c(24991561L, 18283812L, 22380688L, 
12160990L, 30266973L, 19213004L, 27026700L, 22573486L, 21743899L, 
16178486L), pc_no_match = c(172167L, 123538L, 144297L, 73969L, 
848697L, 385718L, 424367L, 392399L, 87832L, 65499L), pc_r_multi_3p = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), pc_r_multi_5p = c(37313L, 
26587L, 35617L, 20454L, 50475L, 36288L, 51256L, 42501L, 2874L, 
2164L), pc_r_open_3p = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), pc_r_open_5p = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
    pc_swap = c(7669869L, 5552178L, 6851657L, 3696743L, 9264859L, 
    5820334L, 8154418L, 6915622L, 460513L, 351558L), unmapped_multihit = c(98407, 
    74100, 91096, 46394, 116268, 73133, 111245, 97857, 6414, 
    4988), unmapped_no_match = c(3134130, 2346361, 2846190, 1526810, 
    7195663, 4283275, 5740873, 4352029, 1898339, 1346846), mapped_incorrect = c(7670780L, 
    5552834L, 6852432L, 3697845L, 9265633L, 5822986L, 8159635L, 
    6915835L, 460641L, 351638L), mapped_correct = c(24991561L, 
    18283812L, 22380688L, 12160990L, 30266973L, 19213004L, 27026700L, 
    22573486L, 21743899L, 16178486L)), row.names = c("A01", "A02", 
"A03", "A06", "A10", "A11", "A12", "B03", "hiseq_pDNA", "miseq_pDNA"
), class = "data.frame")

>Solution :

Removed prior answers:

The key element is to use facet_grid(. ~ group, scales = "free", space = "free")

library(tidyverse)

df %>%
  rownames_to_column(var = "id") %>%
  filter(id != "miseq_pDNA") %>%
  select(id, unmapped_multihit:mapped_correct) %>% 
  mutate(group = c(rep('KAPA', 4), rep('TAKARA', 4), 'plasmid_DNA')) %>%
  pivot_longer(unmapped_multihit:mapped_correct) %>% 
  ggplot(aes(x = id, y = value, fill = name)) + 
  geom_col(position = position_fill(reverse = T)) +
  scale_y_continuous(labels = scales::percent)+
  facet_grid(. ~ group, scales = "free", space = "free")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading