R语言 在ggplot2中的堆叠条形图上绘制数据

e1xvtsh3  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(285)

我在ggplot2中制作堆叠条形图时遇到了一些问题。下面是我的数据:

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

我试图根据每个ID可视化Genus,名称和值。我还尝试了这个页面的代码Showing data values on stacked bar chart in ggplot2,就像我已经尝试过的那样:

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

所需的绘图格式,如

但我每次只能处理一个ID。我有多个ID,也希望按名称着色,例如GroupA使用蓝色(暗到亮)和GroupB使用另一个绿色(暗到亮)。在这方面的任何帮助将不胜感激。谢谢。

xam8gpfp

xam8gpfp1#

这是你想要的输出吗?

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()`).

创建于2023-05-24使用reprex v2.0.2
使用facet_wrap()代替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()`).

创建于2023-05-24带有reprex v2.0.2

相关问题