How to add axis text in this negative and positive bars differently using ggplot2?

后端 未结 3 771
死守一世寂寞
死守一世寂寞 2020-12-19 06:06

I\'ve drawed bar graph with negative and positive bars which is familiar to the research. However, my code seems extremely inconvenient and verbose usinggraphics::plot

3条回答
  •  自闭症患者
    2020-12-19 06:23

    Two options:

    library(ggplot2)
    
    # my data
    df <- data.frame(genus=c("Prevotella","Streptococcus","YRC22","Phascolarctobacterium","SMB53","Epulopiscium",
                             "CF231","Anaerovibrio","Paludibacter","Parabacteroides","Desulfovibrio","Sutterella",
                             "Roseburia","Others__0_5_","Akkermansia","Bifidobacterium","Campylobacter","Fibrobacter",
                             "Coprobacillus","Bulleidia","f_02d06","Dorea","Blautia","Enterococcus","Eubacterium",
                             "p_75_a5","Clostridium","Coprococcus","Oscillospira","Escherichia","Lactobacillus"),
                     class=c(rep("groupA",18),rep("groupB",13)),
                     value=c(4.497311,4.082377,3.578472,3.567310,3.410453,3.390026,
                             3.363542,3.354532,3.335634,3.284165,3.280838,3.218053,
                             3.071454,3.026663,3.021749,3.004152,2.917656,2.811455,
                             -2.997631,-3.074314,-3.117659,-3.151276,-3.170631,-3.194323,
                             -3.225207,-3.274281,-3.299712,-3.299875,-3.689051,-3.692055,
                             -4.733154)
    )
    
    ggplot(df, aes(reorder(genus, -value), value, fill = class)) +
        geom_bar(stat = "identity") +
        coord_flip() +
        geom_text(aes(label = genus,
                      y = ifelse(value < 1, 1.5, -1.5)), size = 2.5) +
        theme(axis.title.y=element_blank(),
              axis.text.y=element_blank(),
              axis.ticks.y=element_blank())
    

    Or this:

    library(ggplot2)
    
    # my data
    df <- data.frame(genus=c("Prevotella","Streptococcus","YRC22","Phascolarctobacterium","SMB53","Epulopiscium",
                             "CF231","Anaerovibrio","Paludibacter","Parabacteroides","Desulfovibrio","Sutterella",
                             "Roseburia","Others__0_5_","Akkermansia","Bifidobacterium","Campylobacter","Fibrobacter",
                             "Coprobacillus","Bulleidia","f_02d06","Dorea","Blautia","Enterococcus","Eubacterium",
                             "p_75_a5","Clostridium","Coprococcus","Oscillospira","Escherichia","Lactobacillus"),
                     class=c(rep("groupA",18),rep("groupB",13)),
                     value=c(4.497311,4.082377,3.578472,3.567310,3.410453,3.390026,
                             3.363542,3.354532,3.335634,3.284165,3.280838,3.218053,
                             3.071454,3.026663,3.021749,3.004152,2.917656,2.811455,
                             -2.997631,-3.074314,-3.117659,-3.151276,-3.170631,-3.194323,
                             -3.225207,-3.274281,-3.299712,-3.299875,-3.689051,-3.692055,
                             -4.733154)
    )
    
    ggplot(df, aes(reorder(genus, -value), value, fill = class)) +
        geom_bar(stat = "identity") +
        coord_flip() +
        xlab("genus")
    

提交回复
热议问题