# gghalves包-你五毛我五毛

## 介绍

`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)
```

mapping

data

stat

position

side

nudge

draw_quantiles

trim

scale

na.rm

show.legend

inherit.aes

geom

bw

## 示例

### 单个函数

```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()
```

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

### geom_half_violin

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

### geom_half_point

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

### 综合案例

#### 云雨图

```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')))
```

```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')
```

```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)
```

```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")
```

```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)
)
```

```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()
```

```# 绘图
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)
```

### 混合图

```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)
```

### 其他参考资料

• Using gghalves--Frederik Tiedemann[3]
• gghalves: Compose Half-Half Plots Using Your Favourite Geoms[4]
• CRAN[5]

### 参考资料

[1]

gghalves: https://github.com/erocoar/gghalves

[2]

[3]

Using gghalves--Frederik Tiedemann: https://erocoar.github.io/gghalves/

[4]

gghalves: Compose Half-Half Plots Using Your Favourite Geoms: https://cran.r-project.org/web/packages/gghalves/index.html

[5]

CRAN: https://rdrr.io/cran/gghalves/f/vignettes/gghalves.Rmd

0 条评论

• ### 可视化中你最常用的软件包有哪些？

看到很多答主都给出了全面的回答，包括数据处理，统计建模等方面。而在这篇推文中，我将对自己较为擅长的领域（R语言可视化）进行详细的介绍。

• ### 走进职业键盘侠的世界，乘风破浪没有谁能阻挡的黑产江湖

他们也曾网上冲浪，随着互联网的迭代变迁，他们激战过天涯、猫扑、西祠胡同，也曾在博客、开心、人人上指点江山，后来他们转战微博、微信、电商。

• ### 基于java的中文分词工具ANSJ

ANSJ 这是一个基于n-Gram+CRF+HMM的中文分词的java实现. 分词速度达到每秒钟大约200万字左右（mac air下测试），准确率能达到96%以...

• ### 能买几颗糖？？

Q：又来买糖，兜里装了一块零一分，即(￥1.01),，糖果也涨价了，变成0.56元一个，问买一个之后兜里还剩多少钱？

• ### 互联网薅羊毛指南

薅羊毛，一种参与各种优惠活动的新型说法，甚至存在薅羊毛hei产，当然这么敏感的话题我就不说了，我们来聊聊近两年互联网的一些羊毛。

• ### 深度学习AI美颜系列----AI人像美妆算法初识

人像美妆是近几年来深受广大女孩儿群体喜欢的修图功能之一，目前市面中做的比较好的有美妆相机、玩美彩妆、天天P图等APP，当然还有一些PC专用的秀图软件，本文将给大...

• ### 外科手术机器人挑战治疗秃头：美国市场一年35亿美元！

在(美国加州)圣荷西的一家外科手术机器人公司Restoration Robotics，引发了大量有关毛囊移植机器人的相关新闻。该公司在10月份宣布他们刚刚出售了...

• ### 你也可以拥有「宋慧乔妆」，美图MakeupGan妆容迁移算法开启个性化妆容时代

虚拟试妆技术一直是美妆、美颜市场最重要的技术之一。当前该领域流行的主流技术为传统素材贴妆，该技术指由专业设计师按指定格式设计好妆容素材，再利用人脸关键点检测把妆...

• ### 动作要快，姿势要帅！新年「薅羊毛」全靠这个小程序

眼看着春节越来越近了，人们都一脸欢欣地准备着年货、车票，在辛勤地工作一年之后，他们终于要迎来团聚放松的节日，迎接新一年的到来。

• ### 员工不知道福利，可能因为你不会做K吧！

哎~刚才领导说我没有很好的整合公司内的福利资源，搞得员工都不知道公司有夜宵福利、健身优惠、小龙虾五折券...

• ### 网购黑产“新五毒”趁机作乱，腾讯云天御实时狙击

憨牛、骏马、绵羊、灵狐、小章鱼，现实中的这些可爱动物，在互联网黑产的推波助澜中，摇身一变，成为黑产“新五毒”。它们借助网购高峰肆虐，给商家、用户造成严重损失。...

• ### 他们让云撸猫变成现实，台湾大学开发手持VR设备解救吸猫人

最近，他们开发了一个名叫HairTouchVR手持设备，可以让人在VR世界中体验真实的撸猫感觉。

• ### 2021 新一年的期望！谈谈数学期望在生活中的应用

2021 年到来了，有时会感叹时间过得真快。在 2020 年，对于大部分人来说都增加了两个额外的东西：口罩和健康码。一些事情的发生，悄然改变了我们的生活，推动着...

• ### 30分钟，教你从0到1搞定一次完整的数据可视化分析！

今天就单独用一篇实操文章来讲解一下如何做一次完整的数据可视化分析，全部过程大约耗时30分钟。