在 ggplot 中需要帮助使用错误栏执行多因素条形图

Need help in ggplot doing multiple factor barplot with error bar

我有一个数据,我想为其绘制一个带有误差条的条形图。 我的数据如下:

dput(level6.top35)
structure(list(patient = structure(c(3L, 3L, 3L, 1L, 1L, 1L, 
4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 
9L, 10L, 10L, 10L, 11L, 11L, 11L, 2L, 2L, 2L), .Label = c("P1", 
"P10", "P11", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9"), class = "factor"), 
    visit = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("V1", "V2", "V3"), class = "factor"), 
    Bacteroides = c(11095L, 9981L, 2426L, 6107L, 14806L, 785L, 
    34127L, 27590L, 4699L, 42464L, 32146L, 321L, 611L, 402L, 
    455L, 5597L, 475L, 2842L, 481L, 11508L, 2125L, 842L, 960L, 
    3215L, 12118L, 10526L, 517L, 67434L, 82449L, 419L, 25643L, 
    4455L), Clostridium = c(53693L, 51961L, 89862L, 1122L, 3987L, 
    3095L, 3083L, 372L, 1628L, 4L, 13L, 11346L, 47803L, 10120L, 
    939L, 2280L, 11355L, 18642L, 4358L, 53L, 47L, 22L, 44L, 1897L, 
    9328L, 4394L, 4886L, 7025L, 175L, 1522L, 14776L, 30405L), 
    Turicibacter = c(25L, 0L, 10L, 9L, 0L, 0L, 4428L, 382L, 827L, 
    18L, 0L, 370L, 106L, 2180L, 5789L, 422L, 4355L, 1585L, 21205L, 
    567L, 131028L, 32389L, 14953L, 50692L, 3666L, 9811L, 1694L, 
    123L, 103L, 475L, 1038L, 0L), Haemophilus = c(31L, 27L, 13L, 
    2693L, 530L, 908L, 103L, 217L, 22L, 21743L, 7413L, 40763L, 
    1303L, 40182L, 52L, 67L, 18501L, 7547L, 28384L, 756L, 19L, 
    43928L, 19930L, 433L, 70L, 952L, 16796L, 4415L, 88L, 0L, 
    4607L, 507L), Streptococcus = c(303L, 160L, 168L, 1205L, 
    8360L, 12927L, 8380L, 1341L, 306L, 865L, 3490L, 137L, 428L, 
    427L, 5215L, 861L, 11635L, 15341L, 7306L, 12963L, 192L, 1646L, 
    2311L, 645L, 9880L, 9314L, 9091L, 6649L, 7283L, 26253L, 21089L, 
    39463L), Intestinibacter = c(14L, 16L, 0L, 17L, 11L, 32L, 
    4991L, 17L, 76L, 13L, 0L, 8182L, 14976L, 8062L, 7529L, 917L, 
    6612L, 14714L, 23287L, 26558L, 32L, 10L, 46L, 18307L, 7201L, 
    11970L, 6983L, 2963L, 2172L, 1812L, 0L, 1115L), Ruminococcus = c(3237L, 
    7853L, 95L, 4209L, 380L, 105L, 4141L, 18344L, 16L, 4000L, 
    2374L, 17L, 690L, 33L, 3393L, 7285L, 259L, 11344L, 69L, 5175L, 
    46L, 13L, 64L, 156L, 8923L, 19573L, 60L, 6626L, 7614L, 188L, 
    998L, 109L), Veillonella = c(630L, 318L, 512L, 302L, 1739L, 
    420L, 779L, 495L, 11L, 538L, 2857L, 338L, 466L, 1777L, 37L, 
    423L, 2597L, 1330L, 457L, 1720L, 239L, 4659L, 1864L, 188L, 
    1062L, 4061L, 279L, 723L, 291L, 11009L, 14337L, 7129L), Sutterella = c(65L, 
    46L, 25L, 27L, 0L, 62L, 20L, 16L, 38L, 8499L, 7987L, 35L, 
    78L, 37L, 21L, 84L, 12L, 238L, 39L, 1746L, 26L, 31L, 65L, 
    383L, 11200L, 565L, 50L, 40L, 17L, 14L, 1407L, 353L), Epulopiscium = c(0L, 
    0L, 0L, 0L, 0L, 12L, 0L, 0L, 0L, 0L, 0L, 14447L, 8925L, 7733L, 
    0L, 6L, 20L, 823L, 158L, 84L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), Faecalibacterium = c(184L, 203L, 154L, 
    113L, 92L, 135L, 111L, 144L, 102L, 1297L, 21410L, 132L, 185L, 
    138L, 127L, 151L, 135L, 204L, 173L, 128L, 203L, 148L, 191L, 
    177L, 169L, 171L, 193L, 150L, 133L, 169L, 4444L, 404L), Bifidobacterium = c(2288L, 
    8161L, 63L, 605L, 169L, 95L, 46L, 71L, 72L, 876L, 2540L, 
    60L, 467L, 73L, 578L, 1537L, 79L, 5413L, 73L, 543L, 127L, 
    86L, 144L, 76L, 775L, 71L, 84L, 80L, 64L, 47L, 49L, 70L), 
    Tyzzerella = c(18L, 0L, 0L, 559L, 0L, 0L, 1408L, 1666L, 0L, 
    86L, 373L, 0L, 373L, 0L, 439L, 235L, 107L, 21L, 0L, 0L, 0L, 
    0L, 25L, 134L, 4126L, 12034L, 4L, 0L, 0L, 0L, 47L, 0L), Lactobacillus = c(0L, 
    0L, 0L, 0L, 0L, 14L, 0L, 0L, 0L, 0L, 0L, 5L, 11L, 4L, 39L, 
    25L, 321L, 56L, 0L, 36L, 0L, 5L, 0L, 5L, 848L, 63L, 0L, 138L, 
    538L, 3801L, 122L, 4373L), Serratia = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 128L, 69L, 0L, 0L, 0L, 0L, 0L, 226L, 0L, 
    0L, 0L, 0L, 0L, 7828L, 0L, 0L, 0L, 0L, 70L, 0L, 0L, 0L, 0L
    ), Rothia = c(0L, 0L, 11L, 6L, 16L, 24L, 0L, 0L, 5L, 0L, 
    0L, 0L, 0L, 10L, 0L, 9L, 11L, 140L, 267L, 175L, 0L, 190L, 
    4617L, 0L, 0L, 0L, 1362L, 19L, 47L, 518L, 21L, 34L), Anaerosporobacter = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 256L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 31L, 3239L, 3546L, 0L, 0L, 
    0L, 0L, 0L, 0L), Erysipelatoclostridium = c(19L, 0L, 7L, 
    184L, 194L, 23L, 320L, 129L, 7L, 1151L, 436L, 20L, 52L, 0L, 
    862L, 1365L, 88L, 20L, 0L, 263L, 9L, 6L, 71L, 46L, 1175L, 
    217L, 0L, 190L, 98L, 0L, 72L, 26L), Paeniclostridium = c(0L, 
    0L, 0L, 0L, 303L, 0L, 0L, 0L, 0L, 0L, 0L, 129L, 9L, 339L, 
    0L, 0L, 66L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 5167L, 228L, 
    221L, 0L, 0L, 0L), Blautia = c(526L, 132L, 101L, 87L, 19L, 
    97L, 93L, 118L, 71L, 204L, 1356L, 70L, 105L, 84L, 71L, 144L, 
    88L, 649L, 136L, 627L, 156L, 88L, 142L, 83L, 139L, 138L, 
    134L, 122L, 81L, 99L, 98L, 125L), Anaerostipes = c(27L, 38L, 
    25L, 20L, 10L, 24L, 17L, 21L, 0L, 709L, 4603L, 23L, 24L, 
    20L, 0L, 178L, 18L, 30L, 42L, 24L, 29L, 16L, 37L, 23L, 57L, 
    39L, 29L, 29L, 16L, 26L, 25L, 27L), Enterococcus = c(31L, 
    32L, 26L, 126L, 68L, 2498L, 70L, 31L, 26L, 0L, 15L, 59L, 
    57L, 23L, 395L, 758L, 133L, 0L, 0L, 27L, 50L, 36L, 56L, 21L, 
    39L, 0L, 422L, 159L, 20L, 24L, 96L, 95L), Citrobacter = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 3583L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 37L, 0L, 0L, 0L, 1088L, 0L, 0L, 0L, 0L, 0L, 144L, 
    0L, 0L, 0L, 0L), Prevotella = c(155L, 168L, 87L, 153L, 95L, 
    121L, 100L, 125L, 152L, 307L, 124L, 100L, 84L, 117L, 91L, 
    168L, 128L, 137L, 130L, 98L, 139L, 114L, 252L, 84L, 159L, 
    106L, 140L, 201L, 114L, 126L, 160L, 125L), Roseburia = c(621L, 
    19L, 0L, 0L, 0L, 0L, 0L, 18L, 0L, 46L, 32L, 17L, 13L, 0L, 
    0L, 36L, 17L, 160L, 0L, 109L, 18L, 15L, 22L, 77L, 1505L, 
    559L, 38L, 26L, 12L, 22L, 849L, 90L), Parabacteroides = c(60L, 
    18L, 12L, 114L, 9L, 49L, 349L, 593L, 60L, 158L, 162L, 46L, 
    53L, 42L, 17L, 33L, 29L, 197L, 49L, 458L, 42L, 45L, 83L, 
    271L, 479L, 429L, 51L, 63L, 76L, 0L, 85L, 47L), Neisseria = c(0L, 
    0L, 0L, 77L, 0L, 0L, 0L, 12L, 0L, 9L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 454L, 2L, 0L, 0L, 771L, 2662L, 4L, 0L, 11L, 10L, 
    0L, 0L, 0L, 0L, 0L), Actinobacillus = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 2670L, 149L, 0L, 0L, 0L, 0L, 0L, 0L, 130L, 
    0L, 0L, 0L, 0L, 0L, 10L, 0L, 60L, 0L, 0L, 0L, 0L, 0L, 0L), 
    Granulicatella = c(59L, 27L, 22L, 18L, 22L, 14L, 19L, 36L, 
    76L, 37L, 0L, 0L, 0L, 61L, 60L, 24L, 93L, 90L, 457L, 60L, 
    52L, 42L, 215L, 0L, 40L, 45L, 665L, 14L, 27L, 260L, 34L, 
    46L), Actinomyces = c(52L, 27L, 12L, 8L, 8L, 16L, 36L, 16L, 
    89L, 12L, 23L, 13L, 0L, 53L, 18L, 0L, 30L, 112L, 624L, 89L, 
    12L, 45L, 116L, 11L, 58L, 12L, 587L, 65L, 47L, 135L, 18L, 
    35L), Lachnoclostridium = c(21L, 19L, 17L, 37L, 0L, 0L, 211L, 
    337L, 13L, 361L, 184L, 0L, 12L, 12L, 19L, 91L, 0L, 66L, 0L, 
    228L, 44L, 9L, 0L, 77L, 293L, 257L, 0L, 0L, 0L, 0L, 28L, 
    20L), Pediococcus = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2101L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 9L, 0L, 0L, 0L, 0L, 0L, 0L), Fusobacterium = c(84L, 
    51L, 55L, 551L, 12L, 19L, 22L, 54L, 23L, 41L, 40L, 21L, 17L, 
    14L, 14L, 78L, 18L, 228L, 88L, 35L, 75L, 43L, 162L, 24L, 
    39L, 25L, 90L, 15L, 21L, 56L, 24L, 36L), Alistipes = c(68L, 
    81L, 24L, 69L, 35L, 66L, 40L, 57L, 60L, 86L, 72L, 48L, 47L, 
    60L, 51L, 92L, 48L, 67L, 72L, 36L, 40L, 65L, 137L, 21L, 31L, 
    65L, 84L, 100L, 93L, 42L, 81L, 41L), Eubacterium = c(0L, 
    7L, 0L, 0L, 0L, 0L, 0L, 0L, 8L, 12L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L)), .Names = c("patient", "visit", "Bacteroides", "Clostridium", 
"Turicibacter", "Haemophilus", "Streptococcus", "Intestinibacter", 
"Ruminococcus", "Veillonella", "Sutterella", "Epulopiscium", 
"Faecalibacterium", "Bifidobacterium", "Tyzzerella", "Lactobacillus", 
"Serratia", "Rothia", "Anaerosporobacter", "Erysipelatoclostridium", 
"Paeniclostridium", "Blautia", "Anaerostipes", "Enterococcus", 
"Citrobacter", "Prevotella", "Roseburia", "Parabacteroides", 
"Neisseria", "Actinobacillus", "Granulicatella", "Actinomyces", 
"Lachnoclostridium", "Pediococcus", "Fusobacterium", "Alistipes", 
"Eubacterium"), class = "data.frame", row.names = c("AA_001_20-4-16", 
"AA_001-V2", "AA_001_19-5-16", "AA_ISS-01-V1", "AA_ISS-01-V2", 
"AA_ISS-01-V3", "AA_ISS-02-V1", "AA_ISS-02-V2", "AA_ISS-02-V3", 
"AA_ISS-03-V1", "AA_ISS-03-V2", "AA_ISS-04-V1", "AA_ISS-04-V2", 
"AA_ISS-04-V3", "AA_ISS-05-V1", "AA_ISS-05-V2", "AA_ISS-05-V3", 
"AA_ISS-06-V1", "AA_ISS-06-V2", "AA_ISS-06-V3", "AA_ISS-07-V1", 
"AA_ISS-07-V2", "AA_ISS-07-V3", "AA_ISS-08-V1", "AA_ISS-08-V2", 
"AA_ISS-08-V3", "AA_ISS-09-V1", "AA_ISS-09-V2", "AA_ISS-09-V3", 
"AA_ISS-10-V1", "AA_ISS-10-V2", "AA_ISS-10-V3")) 

到目前为止,我已尝试重塑数据,但很少有人尝试正确绘制它们。但徒劳无功。

library(reshape2)

df1<-melt(level6.top35, id.vars = c("patient","visit"))
ggplot(data=df1,aes(x=variable,y=value, fill=visit))+geom_bar(position="dodge",stat="identity")
+geom_errorbar( aes(x=variable, ymin=value-sd, ymax=value+sd), width=0.4, colour="orange", alpha=0.9, size=1.3)

我已经成功地完成了情节但没有错误栏。理想情况下,我喜欢有一个带有错误栏的条形图。

您需要汇总数据,然后添加误差线,例如像这样:

df1 <- melt(level6.top35, id.vars = c("patient","visit"))
df1 %>% group_by(visit, variable) %>%
    summarise(SD = sd(value), value = mean(value)) %>% ungroup() %>% 
    ggplot(., aes(x=variable, y=value,  fill = visit)) +
    geom_bar(stat="identity", position = "dodge") +
    geom_errorbar(aes(ymin= value - SD, ymax = value + SD, width=0.2), 
                  position=position_dodge(width=0.90)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1))+ 
    labs(x="Species")

是否只绘制正条、限制 y 轴以避免负值等仍由您决定。