首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >R可视化:ggpubr的基本图形

R可视化:ggpubr的基本图形

原创
作者头像
生信学习者
发布2024-06-11 15:17:00
发布2024-06-11 15:17:00
44600
代码可运行
举报
运行总次数:0
代码可运行

欢迎大家关注全网生信学习者系列:

  • WX公zhong号:生信学习者
  • Xiao hong书:生信学习者
  • 知hu:生信学习者
  • CDSN:生信学习者2

介绍

GGPlot2(通常缩写为ggplot2)是一个在R语言中广泛使用的绘图包,以其灵活和强大的数据可视化功能而闻名。它基于"The Grammar of Graphics"一书的概念,允许用户通过组合不同的视觉元素来创建自定义的图形。而ggpubr是ggplot2的一个扩展包,它进一步简化了图形的创建过程,特别是对于初学者来说,提供了一种更为直观和易于理解的绘图方式。

ggpubr包中包含了许多高级的绘图功能,其中stat_compare_means函数是一个特别有用的工具,它能够对不同的数据组进行假设检验分析,并且将检验结果直接可视化在图形上。这种功能对于科研人员和数据分析师来说非常有价值,因为它不仅提供了统计检验的结论,还通过图形的方式直观地展示了数据间的差异。

安装ggpubr包

代码语言:javascript
代码运行次数:0
运行
复制
# install.packages("ggpubr")
# devtools::devtools::install_github("kassambara/ggpubr")
library(ggpubr)

准备数据

代码语言:javascript
代码运行次数:0
运行
复制
plotdata <- data.frame(sex = factor(rep(c("F", "M"), each=200)),
                   weight = c(rnorm(200, 55), rnorm(200, 58)))

画图

不同类型的可视化图形

密度图density

代码语言:javascript
代码运行次数:0
运行
复制
ggdensity(plotdata, 
          x = "weight",
          add = "mean", 
          rug = TRUE,    # x轴显示分布密度
          color = "sex", 
          fill = "sex",
          palette = c("#00AFBB", "#E7B800"))

柱状图histogram

代码语言:javascript
代码运行次数:0
运行
复制
gghistogram(plotdata, 
            x = "weight",
            bins = 30,
            add = "mean", 
            rug = TRUE,
            color = "sex", 
            fill = "sex",
            palette = c("#00AFBB", "#E7B800"))

箱线图boxplot

代码语言:javascript
代码运行次数:0
运行
复制
df <- ToothGrowth
head(df)
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(df, 
          x = "dose", 
          y = "len",
          color = "dose", 
          palette =c("#00AFBB", "#E7B800", "#FC4E07"),
          add = "jitter", 
          shape = "dose")+
  stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 50) 

小提琴图violin

代码语言:javascript
代码运行次数:0
运行
复制
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)  

点图dotplot

代码语言:javascript
代码运行次数:0
运行
复制
ggdotplot(ToothGrowth, 
          x = "dose", 
          y = "len",
          color = "dose", 
          palette = "jco", 
          binwidth = 1)

有序条形图 ordered bar plots

代码语言:javascript
代码运行次数:0
运行
复制
data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)
head(dfm[, c("name", "wt", "mpg", "cyl")])
​
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

偏差图Deviation graphs

代码语言:javascript
代码运行次数:0
运行
复制
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")])

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",
          rotate = FALSE,
          xlab = FALSE,
          legend.title = "MPG Group")

棒棒糖图 lollipop chart

代码语言:javascript
代码运行次数:0
运行
复制
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

偏差图Deviation graph

代码语言:javascript
代码运行次数:0
运行
复制
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")

散点图scatterplot

代码语言:javascript
代码运行次数:0
运行
复制
df <- datasets::iris
head(df)
ggscatter(df, 
          x = 'Sepal.Width', 
          y = 'Sepal.Length', 
          palette = 'jco', 
          shape = 'Species', 
          add = 'reg.line',
          color = 'Species', 
          conf.int = TRUE)

  • 添加回归线的系数
代码语言:javascript
代码运行次数:0
运行
复制
ggscatter(df, 
          x = 'Sepal.Width', 
          y = 'Sepal.Length', 
          palette = 'jco', 
          shape = 'Species', 
          add = 'reg.line',
          color = 'Species', 
          conf.int = TRUE)+
  stat_cor(aes(color=Species),method = "pearson", label.x = 3)

  • 添加聚类椭圆 concentration ellipses
代码语言:javascript
代码运行次数:0
运行
复制
data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)

p1 <- ggscatter(dfm, 
          x = "wt", 
          y = "mpg",
          color = "cyl", 
          palette = "jco",
          shape = "cyl",
          ellipse = TRUE)
p2 <- ggscatter(dfm, 
                x = "wt", 
                y = "mpg",
                color = "cyl", 
                palette = "jco",
                shape = "cyl",
                ellipse = TRUE,
                ellipse.type = "convex")
cowplot::plot_grid(p1, p2, align = "hv", nrow = 1)

  • 添加mean和stars
代码语言:javascript
代码运行次数:0
运行
复制
ggscatter(dfm, x = "wt", y = "mpg",
          color = "cyl", palette = "jco",
          shape = "cyl",
          ellipse = TRUE, 
          mean.point = TRUE,
          star.plot = TRUE)

  • 显示点标签
代码语言:javascript
代码运行次数:0
运行
复制
dfm$name <- rownames(dfm)
p3 <- ggscatter(dfm, 
          x = "wt", 
          y = "mpg",
          color = "cyl", 
          palette = "jco",
          label = "name",
          repel = TRUE)
p4 <- ggscatter(dfm, 
                x = "wt", 
                y = "mpg",
                color = "cyl", 
                palette = "jco",
                label = "name",
                repel = TRUE,
                label.select = c("Toyota Corolla", "Merc 280", "Duster 360"))
cowplot::plot_grid(p3, p4, align = "hv", nrow = 1)

气泡图bubble plot

代码语言:javascript
代码运行次数:0
运行
复制
ggscatter(dfm, 
          x = "wt", 
          y = "mpg",
          color = "cyl",
          palette = "jco",
          size = "qsec", 
          alpha = 0.5)+
  scale_size(range = c(0.5, 15))    # Adjust the range of points size

连线图 lineplot

代码语言:javascript
代码运行次数:0
运行
复制
p1 <- ggbarplot(ToothGrowth, 
          x = "dose", 
          y = "len", 
          add = "mean_se",
          color = "supp", 
          palette = "jco", 
          position = position_dodge(0.8))+
  stat_compare_means(aes(group = supp), label = "p.signif", label.y = 29)
p2 <- ggline(ToothGrowth, 
       x = "dose", 
       y = "len", 
       add = "mean_se",
       color = "supp", 
       palette = "jco")+
  stat_compare_means(aes(group = supp), label = "p.signif", 
                     label.y = c(16, 25, 29))
cowplot::plot_grid(p1, p2, ncol = 2, align = "hv")

添加边沿图 marginal plots

代码语言:javascript
代码运行次数:0
运行
复制
library(ggExtra)
p <- ggscatter(iris, 
               x = "Sepal.Length", 
               y = "Sepal.Width",
               color = "Species", 
               palette = "jco",
               size = 3, 
               alpha = 0.6)
ggMarginal(p, type = "boxplot")

  • 第二种添加方式: 分别画出三个图,然后进行组合
代码语言:javascript
代码运行次数:0
运行
复制
sp <- ggscatter(iris, 
                x = "Sepal.Length", 
                y = "Sepal.Width",
                color = "Species", 
                palette = "jco",
                size = 3, 
                alpha = 0.6, 
                ggtheme = theme_bw())             

xplot <- ggboxplot(iris, 
                   x = "Species", 
                   y = "Sepal.Length", 
                   color = "Species", 
                   fill = "Species", 
                   palette = "jco",
                   alpha = 0.5, 
                   ggtheme = theme_bw())+ rotate()

yplot <- ggboxplot(iris, 
                   x = "Species", 
                   y = "Sepal.Width",
                   color = "Species", 
                   fill = "Species", 
                   palette = "jco",
                   alpha = 0.5, 
                   ggtheme = theme_bw())


sp <- sp + rremove("legend")
yplot <- yplot + clean_theme() + rremove("legend")
xplot <- xplot + clean_theme() + rremove("legend")
cowplot::plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", 
          rel_widths = c(2, 1), rel_heights = c(1, 2))

  • 上图主图和边沿图之间的space太大,第三种方法能克服这个缺点
代码语言:javascript
代码运行次数:0
运行
复制
library(cowplot) 
# Main plot
pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  ggpubr::color_palette("jco")

# Marginal densities along x axis
xdens <- axis_canvas(pmain, axis = "x")+
  geom_density(data = iris, aes(x = Sepal.Length, fill = Species),
               alpha = 0.7, size = 0.2)+
  ggpubr::fill_palette("jco")

# Marginal densities along y axis
# Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+
  geom_boxplot(data = iris, aes(x = Sepal.Width, fill = Species),
               alpha = 0.7, size = 0.2)+
  coord_flip()+
  ggpubr::fill_palette("jco")

p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
ggdraw(p2)

  • 第四种方法,通过grob设置
代码语言:javascript
代码运行次数:0
运行
复制
# Scatter plot colored by groups ("Species")
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
                color = "Species", palette = "jco",
                size = 3, alpha = 0.6)
# Create box plots of x/y variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Box plot of the x variable
xbp <- ggboxplot(iris$Sepal.Length, width = 0.3, fill = "lightgray") +
  rotate() +
  theme_transparent()
# Box plot of the y variable
ybp <- ggboxplot(iris$Sepal.Width, width = 0.3, fill = "lightgray") +
  theme_transparent()
# Create the external graphical objects
# called a "grop" in Grid terminology
xbp_grob <- ggplotGrob(xbp)
ybp_grob <- ggplotGrob(ybp)
# Place box plots inside the scatter plot
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
xmin <- min(iris$Sepal.Length); xmax <- max(iris$Sepal.Length)
ymin <- min(iris$Sepal.Width); ymax <- max(iris$Sepal.Width)
yoffset <- (1/15)*ymax; xoffset <- (1/15)*xmax
# Insert xbp_grob inside the scatter plot
sp + annotation_custom(grob = xbp_grob, xmin = xmin, xmax = xmax, 
                       ymin = ymin-yoffset, ymax = ymin+yoffset) +
  # Insert ybp_grob inside the scatter plot
  annotation_custom(grob = ybp_grob,
                       xmin = xmin-xoffset, xmax = xmin+xoffset, 
                       ymin = ymin, ymax = ymax)

二维密度图 2d density

代码语言:javascript
代码运行次数:0
运行
复制
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
                color = "lightgray")
p1 <- sp + geom_density_2d()
# Gradient color
p2 <- sp + stat_density_2d(aes(fill = ..level..), geom = "polygon")
# Change gradient color: custom
p3 <- sp + stat_density_2d(aes(fill = ..level..), geom = "polygon")+
  gradient_fill(c("white", "steelblue"))
# Change the gradient color: RColorBrewer palette
p4 <- sp + stat_density_2d(aes(fill = ..level..), geom = "polygon") +
  gradient_fill("YlOrRd")

cowplot::plot_grid(p1, p2, p3, p4, ncol = 2, align = "hv")

混合图

混合表、字体和图

代码语言:javascript
代码运行次数:0
运行
复制
# Density plot of "Sepal.Length"
#::::::::::::::::::::::::::::::::::::::
density.p <- ggdensity(iris, x = "Sepal.Length", 
                       fill = "Species", palette = "jco")
# Draw the summary table of Sepal.Length
#::::::::::::::::::::::::::::::::::::::
# Compute descriptive statistics by groups
stable <- desc_statby(iris, measure.var = "Sepal.Length",
                      grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
# Summary table plot, medium orange theme
stable.p <- ggtexttable(stable, rows = NULL, 
                        theme = ttheme("mOrange"))
# Draw text
#::::::::::::::::::::::::::::::::::::::
text <- paste("iris data set gives the measurements in cm",
              "of the variables sepal length and width",
              "and petal length and width, respectively,",
              "for 50 flowers from each of 3 species of iris.",
             "The species are Iris setosa, versicolor, and virginica.", sep = " ")
text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black")
# Arrange the plots on the same page
ggarrange(density.p, stable.p, text.p, 
          ncol = 1, nrow = 3,
          heights = c(1, 0.5, 0.3))

  • 注释table在图上
代码语言:javascript
代码运行次数:0
运行
复制
density.p <- ggdensity(iris, x = "Sepal.Length", 
                       fill = "Species", palette = "jco")

stable <- desc_statby(iris, measure.var = "Sepal.Length",
                      grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- ggtexttable(stable, rows = NULL, 
                        theme = ttheme("mOrange"))
density.p + annotation_custom(ggplotGrob(stable.p),
                              xmin = 5.5, ymin = 0.7,
                              xmax = 8)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装ggpubr包
  • 准备数据
  • 画图
    • 密度图density
    • 柱状图histogram
    • 箱线图boxplot
    • 小提琴图violin
    • 点图dotplot
    • 有序条形图 ordered bar plots
    • 偏差图Deviation graphs
    • 棒棒糖图 lollipop chart
    • 偏差图Deviation graph
    • 散点图scatterplot
    • 气泡图bubble plot
    • 连线图 lineplot
    • 添加边沿图 marginal plots
    • 二维密度图 2d density
    • 混合图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档