R版本与运行环境信息
Date:2021-7-21
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
#devtools::install_github('erocoar/gghalves')
library(gghalves)
library(tidyverse)
library(ggsci)
plot_theme <-
theme_bw()+
theme(axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text = element_text(size = 13,color = "black"))
#使用自带的iris数据集
df <- iris %>% as_tibble() %>% select(Species,Sepal.Length,Petal.Length)
df.mean <-
df %>% group_by(Species) %>% summarise(mean=mean(Sepal.Length),
sd = sd(Sepal.Length))
查看数据
> df
# A tibble: 150 x 3
Species Sepal.Length Petal.Length
<fct> <dbl> <dbl>
1 setosa 5.1 1.4
2 setosa 4.9 1.4
3 setosa 4.7 1.3
4 setosa 4.6 1.5
5 setosa 5 1.4
6 setosa 5.4 1.7
7 setosa 4.6 1.4
8 setosa 5 1.5
9 setosa 4.4 1.4
10 setosa 4.9 1.5
> df.mean
# A tibble: 3 x 3
Species mean sd
<fct> <dbl> <dbl>
1 setosa 5.01 0.352
2 versicolor 5.94 0.516
3 virginica 6.59 0.636
绘图
ggplot(df,mapping = aes(Species,Sepal.Length,fill = Species)) +
geom_half_boxplot(outlier.alpha = 0.8,
width = 0.3,
errorbar.length = 0.1 ) +
geom_half_violin(side = "r",
position=position_nudge(x = .12, y = 0),
trim = F) +
geom_half_point(aes(color = Species),
position = position_nudge(x = -.5, y = 0),
size = 1,
alpha = 0.8,
transformation = position_jitter(),
side = "r") +
geom_errorbar(df.mean,
mapping = aes(x=Species,y=mean,ymax = mean + sd,ymin = mean - sd,color = Species),
position = position_nudge(x = 0.06, y = 0),
width = 0.05) +
geom_point(df.mean,
mapping = aes(x=Species,y=mean,color = Species),
position = position_nudge(x = 0.06, y = 0)) +
coord_flip() +
scale_fill_npg()+
scale_color_npg()+
plot_theme +
ggtitle("Test of gghalf",subtitle = "test")