前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >半图绘制,加倍美观!

半图绘制,加倍美观!

作者头像
作图丫
发布2022-03-29 13:45:27
7090
发布2022-03-29 13:45:27
举报
文章被收录于专栏:作图丫作图丫

导语

GUIDE ╲

许多数据的可视化形式都是对称的,例如箱型图、散点图、小提琴图等。由于显示信息的空间有限,可以通过将几何图形切成两半并添加其他几何图形来更好地利用空间。

简介

在2018年发表在nature上的一篇文章中使用了这样的绘图形式,结合了箱型图和散点图,去更好的展示数据的分布,作为ggplot2的补充包之一,gghalves包的geom_half_boxplot,geom_half_violin等函数可以在其基础上绘制一半的图,并且将其拼凑起来,也就是我们今天要介绍的:half-half plot.

Fig2.Biomechanics of predator–prey arms race in lion, zebra, cheetah and impala.nature.2018

gghalves安装

代码语言:javascript
复制
##通过bioconductor安装R包
BiocManager::install("gghalves")
library(gghalves)

可视化简介

01

函数参数

代码语言:javascript
复制
#小提琴图举例
geom_half_violin( ###通过aes()指定图形属性映射。默认为NULL,使用ggplot()中aes()指定的映射
                   mapping = NULL,  
                   ##指定数据框
                   data = NULL,
                   ##覆盖geom_density()和stat_density()之间的默认连接
                   stat = "half_ydensity",
                   ##位置调整,可以是字符串,默认为"dodge"
                   position = "dodge",
                   ##画半小提琴图的一侧,l代表左,r代表右,默认为l
                   side = "l", 
                   ##在小提琴图和分配给x轴上给定因子的空间中间之间添加空间
                   nudge = 0,
                   #在给定的密度估计分位数处绘制水平线
                   draw_quantiles = NULL,
                   ##将小提琴的尾部修整到数据范围
                   trim = TRUE, 
                   ##调整小提琴的面积
                   scale = "area",
                   ##是否删除缺失值
                   na.rm = FALSE,
                   ##是否显示该图层的图例
                   show.legend = NA, 
                   inherit.aes = TRUE)

02

与正常的geom_point进行比较

通过正常的geom_point()绘制散点图,点会恰好对应到横坐标处,而使用geom_half_point()绘制散点图,点会分布在横坐标的半部分,从而将剩余空间留给其它图形。

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
  theme_bw()+
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+##去背景
  geom_half_point()##一半的散点图

  ggplot(iris, aes(x = Species, y = Sepal.Width)) + 
    theme_bw()+
    theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
  geom_point()##正常散点图

03

geom_half_point_panel

与geom_half_point相似,但有一点点不同,geom_half_point_panel可以根据因子水平为点着色,同时在相同的空间对它们进行位置变换。

代码语言:javascript
复制
ggplot(iris, aes(y = Sepal.Width)) +
    geom_half_boxplot() +
    theme_bw()+
    theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
    ###根据种类上色
    geom_half_point_panel(aes(x = 0.5, color = Species),
                          transformation = position_jitter())
  BiocManager::install("ggbeeswarm")
library(ggbeeswarm)
    ggplot(iris, aes(y = Sepal.Width)) +
    geom_half_boxplot() +
    theme_bw()+
    theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
    ###position_quasirandom获取点偏移后的位置
    geom_half_point_panel(aes(x = 0.5, color = Species),
                          transformation = ggbeeswarm:::position_quasirandom())

04

geom_half_dotplot

代码语言:javascript
复制
###小提琴图结合点图  
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
    geom_half_violin() + 
    geom_dotplot(binaxis = "y", method="histodot", stackdir="up")

在这里dotplot()是可以看做half plot的,那么为什么要使用geom_half_dotplot函数呢?因为简单的dotplot函数不支持样本具有多个因素的分类,会导致两种类型的图重叠,例子如下:

代码语言:javascript
复制
###定义一个数据集,每个样本有genotype分类和性别分类
  df <- data.frame(score = rgamma(150, 4, 1), 
                   gender = sample(c("M", "F"), 150, replace = TRUE), 
                   genotype = factor(sample(1:3, 150, replace = TRUE)))
  
  ##使用正常的dotplot函数
  ggplot(df, aes(x = genotype, y = score, fill = gender)) +
    geom_half_violin() + 
    geom_dotplot(binaxis = "y", method="histodot", stackdir="up", position = PositionDodge)
  
  ##使用geom_half_dotplot函数
  ggplot(df, aes(x = genotype, y = score, fill = gender)) +
    geom_half_violin() + 
    geom_half_dotplot(method="histodot", stackdir="up")

05

gghalves和不同的geom联合使用

代码语言:javascript
复制
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")

文章参考:https://github.com/erocoar/gghalves

小编总结

联合使用不同的图形对数据进行可视化,不但可以更准确的展示我们的数据类型,同时也能让我们的绘图结果更加美观!今天小编给大家带来的R包gghalves让我们能够更灵活使用各种绘图,大家学会了吗?

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作图丫 微信公众号,前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档