前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你一半我一半的gghalves

你一半我一半的gghalves

作者头像
医学和生信笔记
发布2022-11-15 11:30:02
4720
发布2022-11-15 11:30:02
举报

安装

代码语言:javascript
复制
# 2选1
install.packages("gghalves")

devtools::install_github('erocoar/gghalves')

使用

主要是添加了3种half geoms:boxplot,violin,point

geomhalfpoint

代码语言:javascript
复制
library(gghalves)
## Loading required package: ggplot2

ggplot(iris, aes(Species, Sepal.Width)) + 
  geom_half_point()

plot of chunk unnamed-chunk-2

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_point(transformation = PositionIdentity)

plot of chunk unnamed-chunk-3

geom_half_point_panel

代码语言:javascript
复制
ggplot(iris, aes(y = Sepal.Width)) +
  geom_half_boxplot() +
  geom_half_point_panel(aes(x = 0.5, color = Species), range_scale = .5)

plot of chunk unnamed-chunk-4

geomhalfboxplot

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_boxplot()

plot of chunk unnamed-chunk-5

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_boxplot(side = "r", center = TRUE, errorbar.draw = FALSE)

plot of chunk unnamed-chunk-6

geomhalfviolin

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_violin()

plot of chunk unnamed-chunk-7

代码语言:javascript
复制
ggplot() +
  geom_half_violin(
    data = ToothGrowth, 
    aes(x = as.factor(dose), y = len, split = supp, fill = supp),
    position = "identity"
  )

plot of chunk unnamed-chunk-8

geomhalfdotplot

代码语言:javascript
复制
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_violin() + 
  geom_dotplot(binaxis = "y", method="histodot", stackdir="up")
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

plot of chunk unnamed-chunk-9

代码语言:javascript
复制
df <- data.frame(score = rgamma(150, 4, 1), 
                 gender = sample(c("M", "F"), 150, replace = TRUE), 
                genotype = factor(sample(1:3, 150, replace = TRUE)))
代码语言:javascript
复制
ggplot(df, aes(x = genotype, y = score, fill = gender)) +
  geom_half_violin() + 
  geom_dotplot(binaxis = "y", method="histodot", stackdir="up", position = PositionDodge)
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

plot of chunk unnamed-chunk-11

代码语言:javascript
复制
ggplot(df, aes(x = genotype, y = score, fill = gender)) +
  geom_half_violin() + 
  geom_half_dotplot(method="histodot", stackdir="up")
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

plot of chunk unnamed-chunk-12

自由组合

代码语言:javascript
复制
suppressPackageStartupMessages(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")
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

plot of chunk unnamed-chunk-13

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

本文分享自 医学和生信笔记 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 使用
  • geomhalfpoint
  • geom_half_point_panel
  • geomhalfboxplot
  • geomhalfviolin
  • geomhalfdotplot
  • 自由组合
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档