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

后端 未结 3 772
死守一世寂寞
死守一世寂寞 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:17

    Here's a solution using dplyr to create some extra columns for the label position and the justification, and then theming the plot to match reasonably closely what you originally had:

    library("dplyr")
    library("ggplot2")
    df <- df %>%
      mutate(
        genus = factor(genus, levels = genus[order(value, decreasing = TRUE)]),
        label_y = ifelse(value < 0, 0.2, -0.2),
        label_hjust = ifelse(value < 0, 0, 1)
      )
    
    my_plot <- ggplot(df, aes(x = genus, y = value, fill = class)) +
      geom_bar(stat = "identity", col = "black") +
      geom_text(aes(y = label_y, label = genus, hjust = label_hjust)) +
      coord_flip() +
      scale_fill_manual(values = c(groupA = "forestgreen", groupB = "goldenrod")) +
      theme_minimal() +
      theme(axis.text.y = element_blank(),
            axis.ticks.y = element_blank(),
            axis.title.y = element_blank(),
            legend.position = "top",
            legend.justification = 0.05,
            legend.title = element_blank(),
            panel.grid.major.y = element_blank(),
            panel.grid.minor.y = element_blank(),
            panel.grid.major.x = element_line(colour = "grey80", linetype = "dashed"),
            panel.grid.minor.x = element_blank()) +
      scale_y_continuous(expression(log[10](italic("LDA score"))),
                         breaks = -6:6, limits = c(-6, 6))
    
    print(my_plot)
    ggsave("lefse.tiff", width = 5, height = 5, dpi = 400, my_plot)
    

    0 讨论(0)
  • 2020-12-19 06:22

    I would try this:

    library(ggplot2)
    
    # change the factor levels so it will be displayed in correct order
    df$genus <- factor(df$genus, levels = as.character(df$genus))
    
    ggplot(df, aes(x = genus, y = value)) +
        geom_bar(aes(fill = class), stat = 'identity') +  # color by class
        coord_flip() +  # horizontal bars
        geom_text(aes(y = 0, label = genus, hjust = as.numeric(value > 0))) +  # label text based on value
        theme(axis.text.y = element_blank())
    

    In the above, hjust will change the direction of the text relative to its y position (flipped to x now), which is similar to pos parameter in base R plot. So you code could also be simplified with a vector for pos argument to text function.

    0 讨论(0)
  • 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")
    

    0 讨论(0)
提交回复
热议问题