ggpubr画图学习

2021-11-10  本文已影响0人  斩毛毛

一款基于ggplot2的可视化包ggpubr

本文基于关于改包的说明文档进行练习

#构建数据集
set.seed(1234)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata)
sex   weight
1   F 53.79293
2   F 55.27743
3   F 56.08444
4   F 52.65430
5   F 55.42912
6   F 55.50606
ggdensity(wdata, x = "weight",
          add = "mean", rug = TRUE,
          color = "sex", fill = "sex",
          palette = c("#00AFBB", "#E7B800"))

# add: mean or median
# rug:是否添加地毯线

还是用上述数据集
(1) 直方图

gghistogram(wdata, x = "weight",
            add = "mean", rug = TRUE,
            color = "sex", fill = "sex",
            palette = c("#00AFBB", "#E7B800"))
gghistogram

(2) 还可以和密度图进行结合

gghistogram(wdata, x = "weight",
            add = "mean", rug = TRUE,
            fill = "sex", palette = c("#00AFBB", "#E7B800"),
            add_density = TRUE)

(1)基础画图

# 构建数据集
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

## 画图
ggbarplot(df, "dose", "len",
          fill = "dose", color = "dose",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"), label = TRUE, 
          lab.pos = "in", lab.col = "white",
          orientation = "horiz",order = c("D2", "D1", "D0.5"))
# palette,添加颜色, 可以添加期刊颜色,

“simpsons” and “rickandmorty”'
# label:  是否添加数值
# lab.co: label的颜色
# lab.pos: label的位置,in or out
# orientation:  水平or 垂直
# order:  柱子的顺序

\color{red}{palette} 可以应用不用期刊的颜色,比如‘npg’ 是就是nature迁颜色,其余还有 “npg”, “aaas”, “lancet”, “jco”, “ucscgb”, “uchicago”,

(2) 多个组进行画图

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

(1) 显示每一个组中的值的大小

ggbarplot(df3, x = "dose", y = "len")

每一个dose中,均有多个len值组成,可以根据add 参数进行不同值的现实

(2) 添加error bar

ggbarplot(df3, x = "dose", y = "len",
          add = "mean_se" (error_bar),
           label = TRUE, lab.vjust = -1.6, 
          fill='supp',
          position = position_dodge() ## 没有该参数为堆积图)

\color{red}{position = position_dodge()} 调整是否为堆积图

add 可以添加的值有none", "dotplot", "jitter", "boxplot", "point", "mean", "mean_se", "mean_sd", "mean_ci", "mean_range", "median", "median_iqr", "median_hilow", "median_q1q3", "median_mad", "median_range


(3) 误差线
误差线可以选择上部分,通过
error.plot = "upper_errorbar" 设置
同时添加误差线和点
add = c("mean_se", "dotplot")


\color{red}{排序}, 通过sort.val 和sort.by.groups 进行设置,sort.val决定是排序大小'asc' or 'desc'; sort.by.groups 布尔值,是否以组进行排序

(1) box plot

p <- ggboxplot(df, "dose", "len",
          fill = "dose", palette = 'npg', ``
          add ='jitter', ## 添加点
          shape ='dose')

(2) 添加p值

## 不同组进行比较
my_comparisons <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2"))
p+stat_compare_means(comparisons = my_comparisons, label = "p.signif")+   stat_compare_means(label.y = 50)

# 删除label = "p.signif" 则以数值形式显示显著性

(3) violin

ggviolin(df, "dose", "len", fill = "dose",
         palette = c("#00AFBB", "#E7B800", "#FC4E07"),
         add = "boxplot", add.params = list(fill = "white"))

数据载入

df2 <- mtcars
df2$name <- rownames(df2) 
df2$cyl <- factor(df2$cyl); 数值变为factor
ggdotchart(df2, 'name','mpg',color = 'cyl',palette = 'aaas',
           add = 'segments', # 添加点下面的线
           sorting = 'ascending', # 排序descending
           dot.size = 6, #点的大小
           label = round(df2$mpg),## 显示label
           font.label = list(color='white',size=9,vjust=0.5))
上一篇下一篇

猜你喜欢

热点阅读