1

I have some problems with making a stacked bar chart in ggplot2.Here is my data look like:

   ID  Genus                          name    value
 1 JP  Dermatophagoides               GroupA  0.997  
 2 JP  Dermatophagoides               GroupB  NA      
 3 JP  Dermatophagoides evansi        GroupA  0.00259
 4 JP  Dermatophagoides evansi        GroupB  NA      
 5 AM  Dermatophagoides farinae       GroupA  0.302  
 6 AM  Dermatophagoides farinae       GroupB  0.303  
 7 AM  Dermatopha microceras          GroupA  0.650  
 8 AM  Dermatopha microceras          GroupB  0.653  
 9 AM  N/A                            GroupA  0.00915
10 AM  N/A                            GroupB  0.00642
11 AS  Cohorte Bimichaeliina          GroupA  0.795 
12 AS  Cohorte Bimichaeliina          GroupB  0.796 
13 AS  Nematalycoidea                 GroupA  0.0120
14 AS  Nematalycoidea                 GroupB  0.0123
15 AS  Possibly Cohorte Bimichaeliina GroupA  0.193 
16 AS  Possibly Cohorte Bimichaeliina GroupB  0.191 

I am trying to visualize Genus,name and value based on every ID.I also tried the code at this page Showing data values on stacked bar chart in ggplot2, like I have tried :

df1 <- df %>% select(ID == "JP")
ggplot(dt1, aes(name, value, fill = Genus)) +
  geom_col()

Desired format of plots like

enter image description here

But I only can handle one ID each time.I have multiple IDs,also want colored by name, for example GroupA using blue(dark to light) and GroupB using another Green(dark to light).Any help in this regard will be highly appreciated. Thanks.

mashimena
  • 165
  • 6

1 Answers1

0

Is this your desired output?

library(tidyverse)
library(ggnewscale)

df <- read.table(text = "ID  Genus                          name    value
 1 JP  Dermatophagoides               GroupA  0.997  
 2 JP  Dermatophagoides               GroupB  NA      
 3 JP  'Dermatophagoides evansi'        GroupA  0.00259
 4 JP  'Dermatophagoides evansi'        GroupB  NA      
 5 AM  'Dermatophagoides farinae'       GroupA  0.302  
 6 AM  'Dermatophagoides farinae'       GroupB  0.303  
 7 AM  'Dermatopha microceras'          GroupA  0.650  
 8 AM  'Dermatopha microceras'          GroupB  0.653  
 9 AM  N/A                            GroupA  0.00915
10 AM  N/A                            GroupB  0.00642
11 AS  'Cohorte Bimichaeliina'          GroupA  0.795 
12 AS  'Cohorte Bimichaeliina'          GroupB  0.796 
13 AS  Nematalycoidea                 GroupA  0.0120
14 AS  Nematalycoidea                 GroupB  0.0123
15 AS  'Possibly Cohorte Bimichaeliina' GroupA  0.193 
16 AS  'Possibly Cohorte Bimichaeliina' GroupB  0.191 ",
header = TRUE, na.strings = "N/A")

df %>%
  mutate(value = parse_number(value)) %>%
  ggplot() +
  geom_col(data = . %>% filter(name == "GroupA"),
           aes(x = name, y = value, fill = Genus),
           position="fill") +
  scale_fill_brewer(palette = "Blues") +
  new_scale_fill() +
  geom_col(data = . %>% filter(name == "GroupB"), 
           aes(x = name, y = value, fill = Genus),
           position="fill") +
  scale_fill_brewer(palette = "Greens") +
  facet_grid(cols = vars(name), rows = vars(ID), scales = "free")
#> Warning: Removed 2 rows containing missing values (`position_stack()`).

Created on 2023-05-24 with reprex v2.0.2


With facet_wrap() instead of facet_grid():

library(tidyverse)
library(ggnewscale)

df <- read.table(text = "ID  Genus                          name    value
 1 JP  Dermatophagoides               GroupA  0.997  
 2 JP  Dermatophagoides               GroupB  NA      
 3 JP  'Dermatophagoides evansi'        GroupA  0.00259
 4 JP  'Dermatophagoides evansi'        GroupB  NA      
 5 AM  'Dermatophagoides farinae'       GroupA  0.302  
 6 AM  'Dermatophagoides farinae'       GroupB  0.303  
 7 AM  'Dermatopha microceras'          GroupA  0.650  
 8 AM  'Dermatopha microceras'          GroupB  0.653  
 9 AM  N/A                            GroupA  0.00915
10 AM  N/A                            GroupB  0.00642
11 AS  'Cohorte Bimichaeliina'          GroupA  0.795 
12 AS  'Cohorte Bimichaeliina'          GroupB  0.796 
13 AS  Nematalycoidea                 GroupA  0.0120
14 AS  Nematalycoidea                 GroupB  0.0123
15 AS  'Possibly Cohorte Bimichaeliina' GroupA  0.193 
16 AS  'Possibly Cohorte Bimichaeliina' GroupB  0.191 ",
header = TRUE, na.strings = "N/A")

df %>%
  mutate(value = parse_number(value)) %>%
  ggplot() +
  geom_col(data = . %>% filter(name == "GroupA"),
           aes(x = name, y = value, fill = Genus),
           position="fill") +
  scale_fill_brewer(palette = "Blues") +
  new_scale_fill() +
  geom_col(data = . %>% filter(name == "GroupB"), 
           aes(x = name, y = value, fill = Genus),
           position="fill") +
  scale_fill_brewer(palette = "Greens") +
  facet_wrap(~ID)
#> Warning: Removed 2 rows containing missing values (`position_stack()`).

Created on 2023-05-24 with reprex v2.0.2

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46