查看原文
其他

你一半我一半的gghalves

阿越就是我 医学和生信笔记 2023-06-15
关注公众号,发送R语言python,可获取资料

💡专注R语言在🩺生物医学中的使用


安装

# 2选1
install.packages("gghalves")

devtools::install_github('erocoar/gghalves')

使用

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

geomhalfpoint

library(gghalves)
## Loading required package: ggplot2

ggplot(iris, aes(Species, Sepal.Width)) + 
  geom_half_point()
plot of chunk unnamed-chunk-2
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_point(transformation = PositionIdentity)
plot of chunk unnamed-chunk-3

geom_half_point_panel

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

ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_boxplot()
plot of chunk unnamed-chunk-5
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

ggplot(iris, aes(x = Species, y = Sepal.Width)) +
  geom_half_violin()
plot of chunk unnamed-chunk-7
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

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
df <- data.frame(score = rgamma(15041), 
                 gender = sample(c("M""F"), 150, replace = TRUE), 
                genotype = factor(sample(1:3150, replace = TRUE)))
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
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

自由组合

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





获取更多信息,欢迎加入🐧QQ交流群:613637742


医学和生信笔记,专注R语言在临床医学中的使用、R语言数据分析和可视化。主要分享R语言做医学统计学、meta分析、网络药理学、临床预测模型、机器学习、生物信息学等。


往期回顾




R语言机器学习R包:mlr3(合辑)


R语言和医学统计学(合辑)


使用R语言画森林图和误差线(合辑)


ggplot2可视化拷贝数变异CNV的GISTIC score


初学者使用R语言读取、写出文件(csv/txt/excel/rdata等)的注意事项


又是聚类分析可视化!


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存