参考教程:http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/。

这里仅摘抄、记录最基本或者这我自己感兴趣的方面,全面参数的用法在对应函数帮助文档中都有详细介绍。

1
2
library(ggpubr)
library(patchwork)

1、点图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
data("mtcars")
df = mtcars
df$cyl = as.factor(df$cyl)
df$name <- rownames(df)

p1 = ggscatter(df, x = "wt", y = "mpg")

##颜色形状
p2 = ggscatter(df, x = "wt", y = "mpg",
		  color="cyl", 
		  palette = "jco",
		  shape = "cyl",
		  ellipse = TRUE)
##文本注释
p3 = ggscatter(df, x = "wt", y = "mpg",
		  label = "name",
		  # 如不设置label.select,则会给所有点添加文本注释
		  label.select = c("Toyota Corolla", "Merc 280", "Duster 360"),
		  repel = TRUE)

##点的大小
p4 = ggscatter(df, x = "wt", y = "mpg",
		  size = "qsec", 
		  alpha = 0.5) +
  		  scale_size(range = c(0.5, 10)) 
(p1 | p2)/(p3 | p4)
image-20220723155729954
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
p1 = ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line") + 
  stat_cor()

p2 = ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line",
          conf.int = TRUE,                
          add.params = list(color = "blue",         
                            fill = "lightgray")) + 
  stat_cor()


p3 = ggscatter(df, x = "wt", y = "mpg",
          add = "reg.line") + 
  stat_cor(method = "pearson",
           label.x = 3, label.y = 30,
           #label.x.npc =  0.3, label.y.npc = 0.3,
           p.accuracy = 0.001,
           r.accuracy = 0.01,
           label.sep = '\n')

p1 | p2 | p3

image-20220723155859509

2、线图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
##(1)一组样本,每个样本一个点
df <- data.frame(dose=c("D0.5", "D1", "D2"),
   len=c(4.2, 10, 29.5))
df
#   dose  len
# 1 D0.5  4.2
# 2   D1 10.0
# 3   D2 29.5
p1 = ggline(df, x = "dose", y = "len")
#show_line_types()

##(2)两组样本,每个样本一个点
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))
df2
#   supp dose  len
# 1   VC D0.5  6.8
# 2   VC   D1 15.0
# 3   VC   D2 33.0
# 4   OJ D0.5  4.2
# 5   OJ   D1 10.0
# 6   OJ   D2 29.5
p2 = ggline(df2, x = "dose", y = "len",
       linetype = "supp", 
       color = "supp",
       shape = "supp")

p1 | p2
image-20220723160407617
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
##多组样本,每个样本多个点
df3 = ToothGrowth
table(df3$supp, df3$dose)
#    0.5  1  2
# OJ  10 10 10
# VC  10 10 10
p3 = ggline(df3, x = "dose", y = "len", 
	   add = "mean_se")

p4 = ggline(df3, x = "dose", y = "len", 
	   color = "supp", 
	   shape = "supp",
	   linetype = "supp",
	   add = "mean_se")

p3 | p4
image-20220723160901062

3、柱状图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)

##整体排序
p1 = ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # 填充颜色(映射分组变量)
          color = "white",            # 边框颜色      
          sort.val = "desc",          # 降序排列
          sort.by.groups = FALSE,     # 是否组内排序
          x.text.angle = 90)
##分组排序
p2 = ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # 填充颜色(映射分组变量)
          color = "white",            # 边框颜色
          sort.val = "asc",          # 升序排列
          sort.by.groups = TRUE,     # 是否组内排序
          x.text.angle = 90)

p1 | p2
image-20220723161637869
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#对于有正负值的柱状图
p3 = ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           
          color = "white",           
          sort.val = "asc",          
          sort.by.groups = FALSE,    
          x.text.angle = 90)         

p4 = ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",          
          color = "white",           
          sort.val = "desc",         
          sort.by.groups = FALSE,    
          x.text.angle = 90,         
          rotate = TRUE,
          ggtheme = theme_minimal())

p3 | p4
image-20220723162150992
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
##棒棒糖图
p5 = ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                
           sorting = "ascending",                        
           add = "segments")

##cleveland
p6 = ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                
           sorting = "descending",                       
           rotate = TRUE,                                
           y.text.col = TRUE) +  
  theme_cleveland()  

p5 | p6
image-20220723162858333