阅读 24 SEO

【R语言】R package -- ggpubr

ggpubr

ggpubr专门为学术期刊绘图而生,据说是能画出editor最喜欢看的图。

1.安装和加载

由于这个包放在cran上,所以安装也方便,直接install.packages().

install.packages("ggpubr")
library("ggpubr")

2.绘图示例

  • Gene expression data
  • Box plots
  • Violin plots
  • Stripcharts and dot plots
  • Density plots
  • Histogram plots
  • Empirical cumulative density function
  • Quantile - Quantile plot
library(ggpubr)
set.seed(1234)
wdata = data.frame(
   sex = factor(rep(c("F", "M"), each=200)),
   weight = c(rnorm(200, 55), rnorm(200, 58)))#200个随机数,平均数是55
head(wdata, 4)
 #  sex   weight
#1   F 53.79293
#2   F 55.27743
#3   F 56.08444
#4   F 52.65430

2.1 Density plots

# Density plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom palette
ggdensity(wdata, x = "weight",
          add = "mean", rug = TRUE,
          color = "sex", fill = "sex",
          palette = c("#00AFBB", "#E7B800"))

其中,x为横轴需要绘制的变量;add = "mean"表示添加上均值;rug = TRUE表示添加数据小地毯,这样更能展示出数据的分布;color = "sex", fill = "sex",表示密度图的颜色以sex为填充,如果只有color没有fill,则只会展示出密度曲线,不会有填充,反之则密度曲线都用黑线表示;palette作为调色板,指定绘图颜色。

image.png

2.2 Histogram plot

# Histogram plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom color palette
gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

参数与Density plots类似,便不再赘述。


image.png

2.3 Box plots and violin plots

# Load data
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)
   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
# Box plots with jittered points
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline colors by groups: dose
# Use custom color palette
# Add jitter points and change the shape by groups
p <- ggboxplot(df, x = "dose", y = "len",
               color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
               add = "jitter", shape = "dose")
p
image.png

做什么类型的图,都要把参数调成示例的模样,然后再进一步地修改,上述参数意义为以dose为横坐标,len为纵坐标,根据不同的dose填充不同的颜色;add = "jitter"表示在boxplot上添加跳动的点,表示数据的分布;shape = "dose"表示点的形状依据dose。

给横坐标分组添加P值

# Add p-values comparing groups
 # Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 50)                   # Add global p-value

my_comparisons表示添加比较的分组;label.y表示总的P值显示的位置。若去除最后一个stat_compare_means,则不显示整体的P值。

image.png

小提琴图

# Violin plots with box plots inside
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change fill color by groups: dose
# add boxplot with white fill color
ggviolin(df, x = "dose", y = "len", fill = "dose",
         palette = c("#00AFBB", "#E7B800", "#FC4E07"),
         add = "boxplot", add.params = list(fill = "white"))+
  stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels
  stat_compare_means(label.y = 50)                                      # Add global the p-value 

image.png

label中P值有两种表示方式,Allowed values include "p.signif" (shows the significance levels), "p.format" (shows the formatted p value),默认是第二种方式。其中,add = "boxplot"表示在小提琴图上添加箱线图,add.params表示add添加图形的参数,此处为添加白色的箱线图。

2.4 Bar plots

2.4.1Ordered bar plots
ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
)

image.png

fill = "cyl",表示颜色填充按另一个分组来;color = "white"改变柱子的边界,即无色;sort.val = "desc"表示变量按降序排列,其他的选择为 Allowed values are "none" (no sorting), "asc" (for ascending) or "desc" (for descending);sort.by.groups = FALSE表示分好的组并不聚集在一起,若为T,则聚集在一起; x.text.angle = 90X轴的名称逆时针旋转90度。

2.4.2 分组内部排序的barplot

通过设置sort.by.groups = TRUE

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in dscending order
          sort.by.groups = TRUE,      # Sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )
image.png
2.4.3 Deviation graphs

偏差图显示了定量值与参考值的偏差,下面的代码中,我们将从mtcars数据集绘制mpg z-score。
Calculate the z-score of the mpg data:
因子的设定值得学习,因子里面夹带了一个判断语句。

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                     levels = c("low", "high"))
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])

创造一个有序的barplot,并且根据mpg因子的水平上色。

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group"
          )

image.png

legend.title对legend添加名称。

旋转图像。use rotate = TRUE and sort.val = “desc”

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in descending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          legend.title = "MPG Group",
          rotate = TRUE,
          ggtheme = theme_minimal()
          )
image.png

2.5 Dot charts

Lollipop chart(棒棒糖图)
当您有大量的值要可视化时,棒棒图是柱状图的另一种选择。

2.5.1 一般棒棒糖图

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "ascending",                        # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
           )

image.png

add = "segments"添加棒。

2.5.2 水平棒棒糖图

  • Sort in descending order. sorting = “descending”.
  • Rotate the plot vertically, using rotate = TRUE.
  • Sort the mpg value inside each group by using group = “cyl”.
  • Set dot.size to 6.
  • Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )
image.png

group=按组进行排序;dot.size设置点的大小;label将值写在点上,其中round()表示取整;font.label设置点的字体。

2.5.3 棒棒糖图类型的偏差图

  • Use y = “mpg_z”
  • Change segment color and size: add.params = list(color = “lightgray”, size = 2)
ggdotchart(dfm, x = "name", y = "mpg_z",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           add.params = list(color = "lightgray", size = 2), # Change segment color and size
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg_z,1),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
image.png

geom_hline绘制指定的水平线。

2.5.4 Cleveland’s dot plot

说白了就是将Y轴的颜色和图中分组的颜色对应起来。Use y.text.col = TRUE.

image.png

参考链接:
http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/

作者:巩翔宇Ibrahimovic

原文链接:https://www.jianshu.com/p/43967191bb06

文章分类
后端
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐