欢迎关注"R语言数据分析指南"
本节来介绍如何通过ggpval包中的add_pval函数来添加p值,真的不要太简单
add_pval可设置参数如下
add_pval(
ggplot_obj,
pairs = NULL,
test = "wilcox.test",
heights = NULL,
barheight = NULL,
textsize = 5,
pval_text_adj = NULL,
annotation = NULL,
log = FALSE,
pval_star = FALSE,
plotly = FALSE,
fold_change = FALSE,
parse_text = NULL,
response = "infer",
...
)
安装并加载R包
package.list=c("tidyverse","ggpval","ggsci")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
自定义主题
theme_niwot <- function(){
theme(panel.grid.major=element_blank(), # 移除主网格线
panel.grid.minor=element_blank(), # 移除次网格线
panel.background = element_blank(), # 设置背景为空
axis.title.x=element_blank(), # 设置X标题为空
axis.title.y=element_blank(), # 设置Y标题为空
axis.text.x=element_text(color="blue",margin = margin(t = 5)), # 设置X轴文本颜色
axis.text.y=element_text(color="black",margin = margin(r = 5)), # 设置y轴文本颜色
axis.ticks.x=element_line(color="black"), # 设置X轴刻度条颜色
axis.ticks.length.x = unit(-.1, "cm"), # 设置X轴刻度长度及方向
panel.border = element_rect(linetype = "dashed",fill = NA), # 定义边框线条类型
axis.line.x.bottom = element_line(linetype="solid"), # 定义x轴线条类型
plot.margin=unit(c(0.5,0.5,0.5,0.5),units=,"cm"), # 定义图边距离
plot.caption = element_text(size = 12, face = "italic", # 设置脚注
color = "#606F7B", margin = margin(t = 12)),
legend.title = element_blank(), # 图例标题为空
legend.key=element_blank(), # 图例键为空
legend.text = element_text(color="black",size=10), # 定义图例文本
legend.spacing.x=unit(0.1,'cm'), # 定义文本书平距离
legend.key.width=unit(0.5,'cm'), # 定义图例水平大小
legend.key.height=unit(0.5,'cm'), # 定义图例垂直大小
legend.background=element_blank(), # 设置背景为空
legend.box.background=element_rect(colour = "black",linetype = "solid"), # 图例绘制边框
legend.box.margin = margin(1,1,1,1),
legend.position = c(0, 1), legend.justification = c(0, 1))
}
绘制箱图
p <- ggplot(ToothGrowth %>% mutate(dose=as.factor(dose)),
aes(dose,len,fill=dose)) +
stat_boxplot(geom="errorbar",width=0.1,position = position_dodge(0.7))+
geom_boxplot(width = 0.5,show.legend = T,position = position_dodge(0.7)) +
scale_fill_nejm()+
theme_niwot()
添加p值
add_pval(p, pairs = list(c(1,2),c(1,3),c(2,3)),test='wilcox.test',
alternative='two.sided',textsize=2)
绘制分面图
p1 <- ggplot(ToothGrowth %>% mutate(dose=as.factor(dose)),
aes(dose,len,fill=dose)) +
stat_boxplot(geom="errorbar",width=0.1,position = position_dodge(0.7))+
geom_boxplot(width = 0.5,show.legend = T,position = position_dodge(0.7)) +
facet_grid(.~supp,scales = "free",labeller = label_wrap_gen())+
scale_fill_nejm()+
theme_niwot()+
theme(legend.position = "non")
分面添加p值
add_pval(p1, pairs = list(c(1,2)))
p值转换
add_pval(p1, pairs = list(c(1, 2)),
test = 't.test',
alternative = "less",
pval_star = T)
添加注释文本
add_pval(p1, pairs = list(c(1,2)), annotation = "p")
add_pval(p1, pairs = list(c(1,2)), annotation = list("p1","p2"))
通过上面一连串的代码可以看到最终的图形还是很美观的,喜欢的小伙伴欢迎关注公众号R语言数据分析指南
QQ交流群
作者微信

关注下方公众号下回更新不迷路,如需要加入微信交流群,请在菜单栏处添加作者微信,备注单位+方向+姓名即可邀您进群;公众号主要分享生物信息学与R语言数据可视化的经典案例,希望对大家有所帮助
围观!8+ SCI全文复现
ggplot2优雅的绘制气泡地图
手把手教你使用circlize绘制热图
ggplot2带你绘制美美的樱花图
手把手教你使用circlize绘制高端基因组

