R可视化:具有方向性barplot图

2023-03-06  本文已影响0人  生信学习者2

具有方向性的barplot图

library(dplyr)
library(ggplot2)

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)
)

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))

my_plot

Reference

上一篇下一篇

猜你喜欢

热点阅读