gghalves包-你五毛我五毛

简介: gghalves可以通过ggplot2轻松地编写自己想要的一半一半(half-half plots)的图片。比如:在散点旁边显示箱线图、在小提琴图旁边显示点图。

介绍


gghalves可以通过ggplot2轻松地编写自己想要的一半一半(half-half plots)的图片。比如:在散点旁边显示箱线图、在小提琴图旁边显示点图。

gghalves[1]将_half_扩展添加到选定的geom。比如:geom_half_violin()函数,相当于geom_violin()函数的变体,该函数主要作用就是展示一半的小提琴图,然后与其他图形组合。还包含以下函数:

  • geom_half_boxplot
  • geom_half_violin
  • geom_half_point


安装

gghalves通过GitHub安装:

if (!require(devtools)) {
    install.packages('devtools')
}
devtools::install_github('erocoar/gghalves')


函数介绍

geom_half_violin(mapping = NULL, data = NULL, stat = "half_ydensity",
  position = "dodge", ..., side = "l", nudge = 0,
  draw_quantiles = NULL, trim = TRUE, scale = "area",
  na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

其参数包括:翻译来源生信玩家[2]

参数 解释
mapping 通过aes()指定图形属性映射。默认为NULL,使用ggplot()中aes()指定的映射。
data 指定数据框。默认为NULL,使用ggplot()中的数据。
stat 覆盖geom_density()和stat_density()之间的默认连接。
position 位置调整,可以是字符串,默认为"dodge",也可以是位置调整函数的调用结果。
side 画半小提琴图的一侧。“ l”代表左,“ r”代表右,默认为“ l”。
nudge 在小提琴图和分配给x轴上给定因子的空间中间之间添加空间。
draw_quantiles 如果不是MULL(默认为NULL),在给定的密度估计分位数处绘制水平线。
trim 若为TRUE(默认),将小提琴的尾部修整到数据范围。若为FALSE,不修剪尾巴。
scale 如果为"area"(默认),则所有小提琴都具有相同的面积(修剪尾部之前)。
na.rm 如果为FALSE(默认),则会使用警告删除缺失值。如果为TRUE,则会自动删除缺少的值。
show.legend 逻辑值,默认为NA,若为FALSE,不显示该图层的图例; 若为TRUE,则显示该图层的图例。 它也可以是带有名称(图形属性)的逻辑向量,用来选择要显示的图形属性。如show.legend = c(size = TRUE,color = FALSE)表示显示size对应的图例,而不显示color对应的图例。
inherit.aes 默认为TRUE,若为FALSE,覆盖ggplot()中aes()默认属性,而不是与他们组合。
geom 覆盖geom_density()和stat_density()之间的默认连接。
bw 要使用的平滑带宽度。如果是数字,则为平滑内核的标准差。
adjust 多次带宽调整。这使得可以在仍使用带宽估计器的情况下调整带宽。例如,adjust = 1/2表示使用默认带宽的一半。


示例

单个函数

我们以iris数据集作为本例数据,先使用单个函数进行绘制。

if (!require(devtools)) {
  install.packages('devtools')
}
devtools::install_github('erocoar/gghalves')


geom_half_boxplot

library(gghalves)
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + 
  geom_half_boxplot()

T{]`$)HU1%JPILYX2}N2B$3.png

默认为箱子在右,使用center = TRUE将箱子居中。下面函数参数调整类似,就不再绘制结果了,就把最原始的进行展示。

ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + 
  geom_half_boxplot(center = TRUE)

image.gifE_M0)GH{RI8(1$T5$]L{4EL.png


geom_half_violin

ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + 
  geom_half_violin()

(BWV8ZXA@$URT4}11LLS(LC.pngimage.gif


geom_half_point

ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + 
  geom_half_point()

Y)SH{}235XXNJZ69~$O(WP4.png


综合案例

云雨图

该案例来自官网案例,但并没有对代码进行解释。这里小编对代码进行详细解释,喜欢的伙伴,可以按照解释自己理解,并用到自己实际所需的复合图中。

先将数据的统计摘要进行计算存到了summ_iris中,包含了均值,标准差,数量标准误差。iris_plot为所需数据,这里将Species变量设置为因子,因为要用它作为分类变量。

library(tidyverse)
# 统计摘要
summ_iris <- iris %>% 
  group_by(Species) %>% 
  summarise(
    mean = mean(Sepal.Length),
    sd = sd(Sepal.Length),
    n = n()
  ) %>% 
  mutate(se = sd/sqrt(n),
         Species = factor(Species, levels = c('versicolor', 'setosa', 'virginica')))
summ_iris
## # A tibble: 3 x 5
##   Species     mean    sd     n     se
##   <fct>      <dbl> <dbl> <int>  <dbl>
## 1 setosa      5.01 0.352    50 0.0498
## 2 versicolor  5.94 0.516    50 0.0730
## 3 virginica   6.59 0.636    50 0.0899
# 数据转换  
iris_plot <- iris %>% 
  mutate(Species = factor(Species, levels = c('versicolor', 'setosa', 'virginica')))
head(iris_plot)

image.gif]C4]21FW(CHM[%$%MZ)L[BA.png

接下来进行绘图,我们想要得到Species与Sepal.Length的关系,其中Species为离散变量,Sepal.Length为连续变量。并绘制了半边的小提琴图,并将该图往右移了0.15,上下位置不变(position_nudge(x = .15, y = 0)),为了后面绘制其他图形留位置。

library(gghalves)
library(ggsignif)
library(ggsci)
library(ggpubr)
ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                    side = 'r')

image.gif1BJR73@X6B$%A9XZU8}LF)9.png

接下来加入散点图,并使x坐标往左移动0.1(x = as.numeric(Species)-0.1),使用position_jitter使得重复的点分散开。

ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                   side = 'r') +
  geom_point(aes(x = as.numeric(Species)-0.1,
                 y = Sepal.Length,color = Species),
             position = position_jitter(width = .05),size = .25, shape = 20)

LCSD3}`H2{D~Z$}()$@P{XO.png

在原来基础上加入箱子图,位置放在正中间

ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                   adjust=1.5, trim=FALSE, colour=NA, side = 'r') +
  geom_point(aes(x = as.numeric(Species)-0.1,
                 y = Sepal.Length,color = Species),
             position = position_jitter(width = .05),size = .25, shape = 20) +
  geom_boxplot(aes(x = Species,y = Sepal.Length, fill = Species),
               outlier.shape = NA,
               width = .05,
               color = "black")

image.gifVFSWDMIG_~0]TOSP@5YQXSD.png

这里比较有趣的是,作者还通过geom_point和geom_errorbar加入和汇总信息以及对应的误差项。

ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                   adjust=1.5, trim=FALSE, colour=NA, side = 'r') +
  geom_point(aes(x = as.numeric(Species)-0.1,
                 y = Sepal.Length,color = Species),
             position = position_jitter(width = .05),size = .25, shape = 20) +
  geom_boxplot(aes(x = Species,y = Sepal.Length, fill = Species),
               outlier.shape = NA,
               width = .05,
               color = "black")+
  geom_point(data=summ_iris,
             aes(x=Species,y = mean,group = Species, color = Species),
             shape=18,
             size = 1.5,
             position = position_nudge(x = .1,y = 0)) +
  geom_errorbar(data = summ_iris,
                aes(x = Species, y = mean, group = Species, colour = Species,
                    ymin = mean-se, ymax = mean+se),
                width=.05,
                position=position_nudge(x = .1, y = 0)
  )

SSOJXDA_P$M{9Q(C6$6R%JC.png

使用ggsci包的scale_color_aaas(),scale_fill_aaas()将尺度的颜色进行改变(非常好用!)在下面展示另外一种配色(scale_color_jco)

ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                   adjust=1.5, trim=FALSE, colour=NA, side = 'r') +
  geom_point(aes(x = as.numeric(Species)-0.1,
                 y = Sepal.Length,color = Species),
             position = position_jitter(width = .05),size = .25, shape = 20) +
  geom_boxplot(aes(x = Species,y = Sepal.Length, fill = Species),
               outlier.shape = NA,
               width = .05,
               color = "black")+
  geom_point(data=summ_iris,
             aes(x=Species,y = mean,group = Species, color = Species),
             shape=18,
             size = 1.5,
             position = position_nudge(x = .1,y = 0)) +
  geom_errorbar(data = summ_iris,
                aes(x = Species, y = mean, group = Species, colour = Species,
                    ymin = mean-se, ymax = mean+se),
                width=.05,
                position=position_nudge(x = .1, y = 0)
  ) +
  scale_color_aaas() +
  scale_fill_aaas()

image.gif$15RYI_Y(KP_M(FROR2FKP8.png

最后使用ggpubr包的geom_signif加入显著性结果,ggsave保存图片。

# 绘图
ggplot(iris_plot , aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_half_violin(aes(fill = Species),
                   position = position_nudge(x = .15, y = 0),
                   adjust=1.5, trim=FALSE, colour=NA, side = 'r') +
  geom_point(aes(x = as.numeric(Species)-0.1,
                 y = Sepal.Length,color = Species),
             position = position_jitter(width = .05),size = .25, shape = 20) +
  geom_boxplot(aes(x = Species,y = Sepal.Length, fill = Species),
               outlier.shape = NA,
               width = .05,
               color = "black")+
  geom_point(data=summ_iris,
             aes(x=Species,y = mean,group = Species, color = Species),
             shape=18,
             size = 1.5,
             position = position_nudge(x = .1,y = 0)) +
  geom_errorbar(data = summ_iris,
                aes(x = Species, y = mean, group = Species, colour = Species,
                    ymin = mean-se, ymax = mean+se),
                width=.05,
                position=position_nudge(x = .1, y = 0)
  ) +
  scale_color_jco() +
  scale_fill_jco() +
  geom_signif(comparisons = list(c("versicolor", "setosa"),
                                 c("versicolor", "virginica"),
                                 c("setosa", "virginica")),
              y_position = c(8.2, 8.6, 8.4),
              map_signif_level = c("***" = 0.001, "**" = 0.01, "*" = 0.05)) +
  ggsave('云雨图.pdf', width = 6, height = 8)

R{]`I`NF1YO@9K0[{8OE3}D.png


混合图

最后是混合图,根据自己想要的图,可以自行添加。相信这个代码简单的图给大家学术作图上省了不少时间。

library(tidyverse)
ggplot() +
  geom_half_boxplot(
    data = iris %>% filter(Species=="setosa"), 
    aes(x = Species, y = Sepal.Length, fill = Species), outlier.color = NA) +
  ggbeeswarm::geom_beeswarm(
    data = iris %>% filter(Species=="setosa"),
    aes(x = Species, y = Sepal.Length, fill = Species, color = Species), beeswarmArgs=list(side=+1)
  ) +
  geom_half_violin(
    data = iris %>% filter(Species=="versicolor"), 
    aes(x = Species, y = Sepal.Length, fill = Species), side="r") +
  geom_half_dotplot(
    data = iris %>% filter(Species=="versicolor"), 
    aes(x = Species, y = Sepal.Length, fill = Species), method="histodot", stackdir="down") +
  geom_half_boxplot(
    data = iris %>% filter(Species=="virginica"), 
    aes(x = Species, y = Sepal.Length, fill = Species), side = "r", errorbar.draw = TRUE,
    outlier.color = NA) +
  geom_half_point(
    data = iris %>% filter(Species=="virginica"), 
    aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side = "l") +
  scale_fill_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) +
  scale_color_manual(values = c("setosa" = "#cba1d2", "versicolor"="#7067CF","virginica"="#B7C0EE")) +
  theme(legend.position = "none") + 
  ggsave('综合图.pdf', width = 6, height = 8)

I@E%2_2{ZVQSIQD8PGN(I]N.png

目录
相关文章
|
10月前
|
人工智能 Linux 数据安全/隐私保护
2026版OpenClaw(Clawdbot)汉化版安装指南:本地+Docker+阿里云多方案落地
2026年初,OpenClaw(前身为Clawdbot/Moltbot)以一周内GitHub Stars从7800飙升至12万+的速度,成为AI领域现象级开源项目。这款本地运行的通用AI助手,打破了传统聊天机器人仅能生成内容的局限,拥有系统文件读写、终端命令执行、浏览器自动化、邮件/日程管理等全权限实操能力,还支持WhatsApp、Telegram等多渠道交互,真正实现“AI帮你干实际活”。但原版全英文界面对中文用户不友好,国内开发者团队完成了全维度汉化,覆盖CLI命令行、Web控制台所有模块,且实现官方更新1小时内同步汉化。
5118 10
|
11月前
|
安全 C语言 Docker
从零开始学构建Docker镜像:4种实用方法+生产级实践规范
本文系统讲解Docker镜像构建四大方法,聚焦生产实践,涵盖环境搭建、Dockerfile规范、多阶段构建、安全管控与CI/CD集成,强调非root运行、镜像瘦身及可追溯Tag规范,助力开发者掌握从开发到生产的全链路容器化技能。
2084 5
|
机器学习/深度学习 数据采集 人工智能
《解锁AI时代必备技能,抢占未来先机》
在科技飞速发展的今天,AI已融入生活的方方面面。掌握AI时代的必备技能尤为重要,包括编程与数据处理、机器学习与深度学习、创新与协作及批判性思维与持续学习。编程语言如Python是关键工具,数据处理能力不可或缺;深入理解机器学习和深度学习能助你成为AI高手;创新思维和团队协作精神助力突破;批判性思维和持续学习确保不被淘汰。掌握这些技能,抓住机遇,实现自我价值,在AI时代乘风破浪。
838 0
|
数据安全/隐私保护
(只需五步)注册谷歌账号详细步骤,解决“此电话号码无法验证”问题
注册google一直不方便,因为如果直接去google官网注册,那么它大概率会显示“此电话号码无法用于进行验证”接下来,按着教程来一步步做,就可以实现跳过此限制,成功用手机号注册google了。很简单的。
51635 1
|
存储 数据挖掘 Linux
探索Linux命令rpm2cpio:解析RPM包内容的利器
`rpm2cpio`是Linux下用于从RPM包中提取内容的工具,它将`.rpm`转换为CPIO归档。无需安装,可直接访问包内文件,适合数据分析。命令简单,常与`cpio`结合使用,如`rpm2cpio package.rpm | cpio -idmv`解压文件。示例包括提取特定文件和列出包内所有文件。注意权限、路径和文件完整性,使用前备份数据,并查阅文档以优化使用。
|
负载均衡 监控 Cloud Native
Service Mesh 的实现原理
【2月更文挑战第20天】
|
人工智能 安全 物联网
构建未来:移动应用开发和操作系统的融合与创新
【7月更文挑战第2天】在数字时代的浪潮中,移动应用开发和操作系统不断进化,推动着技术边界的拓展。本文将探讨移动应用开发的最新趋势、移动操作系统的创新以及它们如何共同塑造我们的数字生活。从跨平台框架的兴起到人工智能的集成,再到操作系统安全性的提升,我们将深入了解这些技术是如何相互影响,并预示着未来可能的发展路径。
|
机器学习/深度学习 人工智能 BI
技术心得:数学建模_饮食计划
技术心得:数学建模_饮食计划
396 0
|
Java 大数据 Linux
Rstudio打不开了怎么办?
Rstudio打不开了怎么办?

热门文章

最新文章