04 ggpubr学习-ggbartplot

525 阅读1分钟

ggbarplot

函数作用:

用于绘制柱形图,与ggplot2中的geom_col或者geom_bar相比,调整图像更加简便

函数参数

image.png image.png image.png

文档中示例

绘制简单的barplot

df <- data.frame(dose=c("D0.5", "D1", "D2"), len=c(4.2, 10, 29.5));print(df)
#   dose  len
# 1 D0.5  4.2
# 2   D1 10.0
# 3   D2 29.5
# Basic plot with label outsite 

修改标签的位置

test <- lapply(c('in', 'out'), FUN = function(pos){
  ggbarplot(df, "dose", "len",
    fill = "steelblue", color = "steelblue",error.plot = "pointrange",
    label = TRUE, lab.pos = pos, lab.col = "black", title = pos)
})
ggarrange(test[[1]], test[[2]], ncol = 2)

image.png

修改颜色

p1 <- ggbarplot(df, "dose", "len", color = "dose",
  palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p2 <- ggbarplot(df, "dose", "len", color = "dose", fill = 'dose',
  palette = c("#00AFBB", "#E7B800", "#FC4E07"))

ggarrange(p1,p2, nrow = 1)

image.png

图像中添加散点或误差线

# 使用数据ToothGrowth
df3 <- ToothGrowth head(df3, 10)#     len supp dose
# 1   4.2   VC  0.5
# 2  11.5   VC  0.5
# 3   7.3   VC  0.5
# 4   5.8   VC  0.5
# 5   6.4   VC  0.5
# 6  10.0   VC  0.5
# 7  11.2   VC  0.5
# 8  11.2   VC  0.5
# 9   5.2   VC  0.5
# 10  7.0   VC  0.5
p1 <- ggbarplot(df3, x = "dose", y = "len")
# Visualize the mean of each group
p2 <- ggbarplot(df3, x = "dose", y = "len",add = "mean")
ggarrange(p1,p2, nrow = 1)

如果不添加add = 'mean', 如左图所示。 image.png err.plot为("pointrange", "linerange", "crossbar", "errorbar", "upper_errorbar", "lower_errorbar", "upper_pointrange", "lower_pointrange", "upper_linerange", "lower_linerange")中的一种类型,在添加err.plot时,add不能为空,必须是"mean_" or "med_" where "*" = sd, se的等中的一种,这里以'mean_se'为示例。

library(patchwork)

types <- c("pointrange", "linerange", "crossbar", "errorbar", 
           "upper_errorbar", "lower_errorbar", "upper_pointrange", "lower_pointrange","upper_linerange", "lower_linerange")

test <- lapply(types, FUN = function(type){
  ggbarplot(df3, x = "dose", y = "len",
    add = "mean_se", error.plot = type, title = type)
})

wrap_plots(test, ncol = 5)

image.png

df <- ToothGrowth

p1 <- ggbarplot(df3, x = "dose", y = "len",
  add = c("mean_se", "jitter"), color = '#00AFBB', fill='#00AFBB', add.params = list(color = 'black', shape = 21, size = 1))
p2 <- ggbarplot(df3, x = "dose", y = "len",
  add = c("mean_se", "point"),color = '#00AFBB', fill='#00AFBB', add.params = list(color = 'black', shape = 21, size = 1))
p3 <- ggbarplot(df3, x = "dose", y = "len",
  add = c("mean_se", "dotplot"),color = '#00AFBB', fill='#00AFBB', add.params = list(color = 'black', shape = 24, size = 1))
p4 <- ggbarplot(df3, x = "dose", y = "len",
  add = c("mean_se", "boxplot"),color = '#00AFBB', fill='#00AFBB', add.params = list(color = 'black', size = 1))

p1|p2|p3|p4

image.png

图像翻转

可以使用orientation = "horiz"或添加ggplot2::coord_flip()

p1 <- ggbarplot(df3, x = "dose", y = "len",
  add = "mean_se", error.plot = "pointrange")

p2 <- ggbarplot(df3, x = "dose", y = "len",
  add = "mean_se", error.plot = "pointrange",  orientation = "horiz")

p3 <- ggbarplot(df3, x = "dose", y = "len",
  add = "mean_se", error.plot = "pointrange")+
  ggplot2::coord_flip()

p1 | p2 | p3

image.png

绘制堆叠,并列柱形图

p1 <- ggbarplot(df3, x = "dose", y = "len",fill = 'supp',color = "supp",
        add = c("mean_se","jitter"), palette = c("#00AFBB", "#E7B800"),
        position = position_dodge())

p2 <- ggbarplot(df3, x = "dose", y = "len",fill = 'supp',color = "black",
        add = c("mean_se", 'point'), palette = c("#00AFBB", "#E7B800"),
        position = position_dodge())

p3 <- ggbarplot(df3, x = "dose", y = "len",fill = 'supp',color = "supp",
        add = c("mean_se", 'point'), palette = c("#00AFBB", "#E7B800"), 
        add.params = list(color='black'),position = position_dodge())

p4 <- ggbarplot(df3, x = "dose", y = "len",fill = 'supp',color = "supp", 
                position = position_fill(),palette = c("#00AFBB", "#E7B800"))

p5 <- ggbarplot(df3, x = "dose", y = "len",fill = 'supp',color = "supp", 
                position = position_stack(),palette = c("#00AFBB", "#E7B800"))

patchwork::wrap_plots(list(p1,p2,p3,p4,p5), ncol = 3)

image.png

排序

# 使用单细胞FindMarkers的结果
df <- rbind(
    my_result_1 %>% dplyr::select(Description,Count,NeLog10Padj,group) %>% arrange(-NeLog10Padj) %>% dplyr::slice(1:10),
    my_result_2 %>% dplyr::select(Description,Count,NeLog10Padj,group) %>% arrange(-NeLog10Padj) %>% dplyr::slice(1:10)
) 
df$group <- factor(df$group, levels = c('NR','R'))

ggbarplot(df, 
          x = "Description",
          y = "NeLog10Padj",
          fill = "group",           
          color = "white",            
          palette = "jco",            
          sort.val = "asc",          #降序
          sort.by.groups = TRUE, 
          top = 30,
          x.text.angle = 90,          
          ylab = " ",
          xlab = " ",
          title = "xxx",
          legend.title = "Group",
          rotate = TRUE,              #纵向
)+ scale_y_continuous(expand = c(0,0))+ 
    theme(plot.title = element_text(hjust = 0.5, size = 15, face = 'bold')) + 
    theme(legend.text = element_blank())

image.png