美文网首页
R可视化:条形图barplot

R可视化:条形图barplot

作者: 生信学习者2 | 来源:发表于2021-08-18 22:35 被阅读0次

前言

条形图和柱状图展示组间差异。更多知识分享请到 https://zouhua.top/

Importing Data

library(dplyr)
library(ggplot2)
library(ggpubr)

grp <- c("ET_B", "ET_P")
grp.col <- c("#FF6000", "#0080C0")

stg <- c("Before", "After")
stg.col <- c("#6288BA", "#C25D97")

BoxPlot

pl1 <- ggplot(data = phen, aes(x=Stage, y=BMI, fill=Stage))+
  stat_boxplot(aes(color=Stage), geom='errorbar',
               width=0.15)+
  geom_boxplot(width=.4)+
  facet_grid(.~Group, scales = "free", space="free")+
  scale_y_continuous(breaks = seq(18, 30, 2),
                     limits = c(17, 32),
                     expand = c(0, 0))+
  stat_compare_means(comparisons = list(stg),
                     method = "wilcox.test",
                     paired = T,
                     label  = "p.signif")+
  scale_fill_manual(values = stg.col)+
  scale_color_manual(values = stg.col)+  
  guides(color=F, fill=F)+
  xlab("")+
  ylab(bquote('BMI ('*kg/m^2*')'))+
  theme_classic()+
  theme(
      axis.title = element_text(size=10, color="black", face="bold"),
      axis.text = element_text(size=9, color="black", face="bold"),
      text = element_text(size=8, color="black"),
      strip.text = element_text(size=9, color="black", face="bold"), 
      panel.grid = element_blank(),
      legend.justification=c(1,0),
      legend.position=c(1, 0))
pl1 

Barplot

pl2 <- phen %>% filter(Stage%in%stg[2]) %>% 
  select(Stage, Group, BMI.k) %>%
  mutate(Stage=factor(Stage, levels = stg),
         Group=factor(Group, levels = grp)) %>%
  Rmisc::summarySE(measurevar="BMI.k", groupvars=c("Stage", "Group")) %>% 
ggplot(aes(x=Group, y=BMI.k, fill=Group)) + 
  geom_bar(stat="identity", width = .7, color="black", size=1) +
  geom_errorbar(aes(ymin=BMI.k-se, ymax=BMI.k),
                  width=.35, color="black", size=1)+
  scale_fill_manual(values = grp.col)+
  scale_y_continuous(breaks = seq(-1, 0, 0.25),
                     limits = c(-1.2, 0),
                     expand = c(0, 0))+
  geom_segment(aes(x = 1, y = -1.1,
                   xend = 2, yend = -1.1),
               size=.5)+
  annotate("text", x=1.5, y=-1.15, label="*", size=7)+
  guides(fill=F)+
  xlab("")+
  scale_x_discrete(position = "top")+ 
  ylab(bquote('Change in BMI ('*kg/m^2*')'))+
  theme_classic()+
  theme(
      axis.title = element_text(size=10, color="black", face="bold"),
      axis.text = element_text(size=9, color="black", face="bold"),
      text = element_text(size=8, color="black"),
      panel.grid = element_blank())
pl2

Combining Figures

cowplot::plot_grid(pl1, pl2,
  ncol = 2, align = "h", axis = "b",
  labels = c("A", "B"))

参考

参考文章如引起任何侵权问题,可以与我联系,谢谢。

相关文章

网友评论

      本文标题:R可视化:条形图barplot

      本文链接:https://www.haomeiwen.com/subject/yovnbltx.html