欢迎关注公众号查看原文
本期我们用ggplot2绘制箱线图,直接上图:
1.加载包
rm(list=ls())
#加载包
library(tidyverse)
library(ggplot2)
library(ggsci)
library(cowplot)
library(openxlsx)
library(ggsignif)##可以加P值,ggstatsplot超强统计包
2.加载数据
setwd("D:/本人信息/生信小书生/R语言绘图/2022-3-13箱线图")#载入路径
data <- read.xlsx("箱线图.xlsx")
3.绘图-箱线图
###箱型图
ggplot(data,aes(x=Name,y=Value))+
geom_boxplot(aes(fill=Name)#,notch = T,notchwidth = 0.7 #变成锯齿状
)+
geom_point(aes(color=Name),position=position_jitter(width=0.15),size=2)+
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示显著性,*---0.05,**---0.01,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主标题
x = 'Name', # x轴的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次标记
caption = 'Data source: LX')+ #添加脚注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y轴线特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x轴线特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 图四周框起来
箱线图-2022-3-13.jpg
4.小提琴图
#####小提琴图----多了一个密度曲线
ggplot(data,aes(x=Name,y=Value))+
geom_violin(aes(fill=Name),alpha=0.7)+
#geom_point(aes(color=Species),position = "jitter")+#加抖动点
geom_point(position = position_jitter(width=0.15),size=1)+###缩小抖动点
geom_boxplot(width=0.1)+
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示显著性,*---0.05,**---0.01,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主标题
x = 'Name', # x轴的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次标记
caption = 'Data source: LX')+ #添加脚注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y轴线特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x轴线特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 图四周框起来
小提琴图-2022-3-13.jpg
5.蜂窝图
#####蜂窝图####
library(ggbeeswarm)
ggplot(data,aes(x=Name,y=Value))+
geom_quasirandom(aes(color=Name),shape=19,
position = "jitter",
method = "smiley")+#加抖动点
geom_signif(
comparisons = list(c("CTRL","TP1")),test="t.test",
tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
map_signif_level = T #用*表示显著性,*---0.05,**---0.01,***---0.001
)+
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主标题
x = 'Name', # x轴的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次标记
caption = 'Data source: LX')+ #添加脚注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y轴线特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x轴线特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 图四周框起来
蜂窝图-2022-3-13.jpg
6.云雨图
###云雨图
library(gghalves)
ggplot(data,aes(x=Name,y=Value))+
geom_half_violin(aes(fill=Name),alpha=0.7)+
geom_point(position = position_jitter(width = 0.2))+#加抖动点
geom_boxplot(width=0.1)+
scale_fill_lancet()+
theme_cowplot()
##调节云
ggplot(data,aes(x=Name,y=Value))+
geom_half_violin(aes(fill=Name),
alpha=0.7,side="r",
adjust=1/2,
position = position_nudge(x=0.2,y=0))+#云往左调整
geom_point(position = position_jitter(width =0.2),size=1)+#加抖动点
geom_boxplot(width=0.1,
position = position_nudge(x=0.2,y=0))+
scale_fill_brewer(palette = "Dark2")+
theme_cowplot()+
coord_flip() + theme(legend.position = c(0.85, 0.3))+###旋转坐标轴
scale_fill_lancet()+
theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
theme_classic()+
ggtitle("boxplot")+
labs(title = 'boxplot', # 添加主标题
x = 'Name', # x轴的名字
y = 'Value',
subtitle = 'Plot of name by value', # 添加次标记
caption = 'Data source: LX')+ #添加脚注
theme(plot.title = element_text(color = 'black', size = 16, hjust = 0.5),
plot.subtitle = element_text(color = 'black', size = 16,hjust = 0.5),
plot.caption = element_text(color = 'black', size = 16,face = 'italic', hjust = 1),
axis.text.x = element_text(color = 'black', size = 16, angle = 0),
axis.text.y = element_text(color = 'black', size = 16, angle = 0),
axis.title.x = element_text(color = 'black', size = 16, angle = 0),
axis.title.y = element_text(color = 'black', size = 16, angle = 90),
legend.title = element_text(color = 'black', size = 16),
legend.text = element_text(color = 'black', size = 16),
axis.line.y = element_line(color = 'black', linetype = 'solid'), # y轴线特征
axis.line.x = element_line (color = 'black',linetype = 'solid'), # x轴线特征
panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA)) # 图四周框起来
云雨图-2022-3-13.jpg
详细数据请参考微信公众号!
网友评论