ggplot2再话箱线图之几何填充
有观众老爷询问如何对箱线图进行几何形状填充,那么今天就来具体介绍一番;在原有的基础上做了一些小的改动也许恰好您正好有此特殊需求,需要着重体会八个字变实为虚,变虚为实,希望对各位观众老爷有所帮助;下面来看具体案例;
需要获取数据的欢迎关注我的号留言
加载R包
library(tidyverse)
library(ggsci)
library(ggprism)
library(rstatix)
library(ggpubr)
library(ggpmisc)
library(ggpattern)
加载数据
gapminder <- read_tsv("gapminder.xls")
数据清洗
df <- gapminder %>%
filter(year %in% c(1957,2002,2007),continent !="Oceania") %>%
select(country,year,lifeExp,continent)%>%
mutate(paired = rep(1:(n()/3),each=3),year=factor(year))
统计分析
df_p_val1 <- df %>% group_by(continent) %>%
wilcox_test(lifeExp ~ year) %>%
adjust_pvalue(p.col = "p", method = "bonferroni") %>%
add_significance(p.col = "p.adj") %>%
add_xy_position(x = "year", dodge = 0.8)
构建填充类型
下面函数来自ggpattern包官方文档
https://coolbutuseless.github.io/package/ggpattern/articles/developing-patterns.html
tiling3_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
args <- as.list(params)
args <- args[grep("^pattern_", names(args))]
# hexagonal tiling using "regular_polygon" pattern
args$pattern <- "polygon_tiling"
# three-color tiling using `fill`, `pattern_fill` and their "average"
avg_col <- gridpattern::mean_col(params$fill, params$pattern_fill)
args$pattern_fill <- c(params$fill, avg_col, args$pattern_fill)
args$x <- boundary_df$x
args$y <- boundary_df$y
args$id <- boundary_df$id
args$prefix <- ""
do.call(gridpattern::patternGrob, args)
}
options(ggpattern_geometry_funcs = list(tiling3 = tiling3_pattern))
数据可视化
df %>%
ggplot(aes(year,lifeExp)) +
stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..),outlier.shape = NA,width=0.5) +
stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..),width=0.2,size=0.35) +
stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..),width=0.2,size=0.35) +
geom_boxplot_pattern(aes(pattern = year,pattern_angle = year,fill=year),
pattern = 'tiling3', pattern_angle = 45,color="black",
pattern_spacing = 0.045,outlier.shape = NA,linetype = "dashed",width=0.5,size=0.35)+
stat_summary(geom = "crossbar", fun = "median",width = 0.5,color="black",size=0.38)+
stat_pvalue_manual(df_p_val1,label = "p.adj.signif",label.size=5,hide.ns = F)+
scale_size_continuous(range=c(1,3))+
# geom_smooth(method = "lm", formula = NULL,size=1,se=T,color="black",linetype="dashed",aes(group=1))+
stat_cor(label.y = 25,aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~"),group=1),color="black",
label.x.npc = "left")+
stat_regline_equation(label.y = 19,aes(group=1),color="black")+
facet_wrap(.~continent,nrow=1)+
scale_fill_npg()+
scale_x_discrete(guide = "prism_bracket")+
scale_y_continuous(limits = c(0,95),minor_breaks = seq(0,95,5),guide = "prism_offset_minor")+
labs(x=NULL,y=NULL)+
theme_prism(base_line_size =0.4)+
theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),units=,"cm"),
strip.text = element_text(size=12),
axis.line = element_line(color = "black",size = 0.4),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(size = 0.2,color = "#e5e5e5"),
axis.text.y = element_text(color="black",size=10),
axis.text.x = element_text(margin = margin(t = -5),color="black",size=10),
legend.position = "none",
panel.spacing = unit(0,"lines"))+
coord_cartesian()
数据获取
还是熟悉的配方,除了构建三色形状填充外,需要体会的依然是变实为虚,变虚为实绘图思维尽在其中,那么本节介绍到此结束;需要数据的留言交流
共有 0 条评论