你一半我一半的gghalves

2022-11-15 11:30:02 浏览数 (1)

安装

代码语言: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

0 人点赞