加载R包
1)设置工作目录
rm(list=ls())#clear Global Environment
setwd('D:\\桌面\\小提琴图')
2)安装、加载R包
#安装包
# install.packages("ggplot2")
# install.packages("ggpubr")
# install.packages("ggsignif")
# install.packages("tidyverse")
# install.packages("ggprism")
# install.packages("vioplot")
#加载包
library(ggplot2)#绘图包
library(ggpubr)#基于ggplot2的可视化包,主要用于绘制符合出版要求的图形
library(ggsignif)#用于P值计算和显著性标记
library(tidyverse)#数据预处理
library(ggprism)#提供了GraphPad prism风格的主题和颜色,主要用于美化我们的图形
library(vioplot)#小提琴图绘制包
准备数据
#可加载自己的数据,这里我们使用随机编写的数据
# df <- read.table("data.txt",header = T, check.names = F)
#自己随机编写的数据
df <- data.frame(
A_1 = c(2,5,6,5,4,8,6,3,8,9),
A_2 = c(5,8,6,3,4,7,9,3,6,4),
B_1 = c(15,10,5,18,12,13,16,14,10,9),
B_2 = c(25,20,23,15,14,24,20,22,25,26),
C_1 = c(1,3,6,5,2,3,6,2,4,1),
C_2 = c(7,8,9,6,7,8,9,6,7,10))
#预览数据
head(df)
image.png
2)处理数据、添加分组信息
#使用tidyverse包对数据进行处理
df <- df %>%
gather(key = 'samples',value = 'values') #gather()函数可以把多列数据合并成一列数据
#添加分组信息
df$group = rep(c("A","B","C"), each = 20)
head(df)#预览数据
image.png
使用vioplot包进行绘图
??vioplot#查看具体参数
vioplot(values~samples, data = df,
main = "vioplot", # 设置标题
col=c("#000000", "#be0027", "#cf8d2e","#e4e932","#2c9f45","#371777"),#设置小提琴颜色
xlab="Samples", ylab="values") # 轴标题
image.png
使用ggplot2包进行绘制
1)基本绘图
p1 <- ggplot(df, aes(x=samples, y=values, fill=samples)) +
geom_violin()
p1
image.png
2)添加箱线图及均值点
p2<-p1+geom_boxplot(alpha=1,outlier.size=0, size=0.3, width=0.2,fill="white")+
stat_summary(fun="mean",geom="point",shape=21, size=2,fill="blue")
p2
image.png
3)自定义颜色
col=c("#000000", "#be0027", "#cf8d2e","#e4e932","#2c9f45","#371777")
p3<-p2+scale_fill_manual(values = col)
p3
image.png
4)按照分组进行分面
p4<-p3+facet_grid(~group,scales = 'free')
p4
image.png
5)显著性标记
p5<-p1+geom_signif(comparisons = list(c("A_1","A_2"),
c("B_1","B_2"),
c("C_1","C_2")),# 设置需要比较的组
map_signif_level = T, #是否使用星号显示
test = t.test, ##计算方法
size=0.8,color="black")
p5
image.png
结合ggprism包进行个性化绘图
p <- ggplot(df, aes(x=samples, y=values, fill=group))+#指定数据
geom_violin(trim = T,position = position_dodge(width = 0.1), scale = 'width')+#绘制小提琴图函数
geom_boxplot(alpha=1,outlier.size=0, size=0.3, width=0.2,fill="white")+#添加箱线图
stat_summary(fun="mean",geom="point",shape=21, size=2,fill="blue")+#均值点
labs(x="Samples",y=NULL)+#标题
# geom_jitter(width = 0.2,size=2,pch=20,color="black")+#添加抖动点
theme_prism(palette = "flames",
base_fontface = "plain", # 字体样式,可选 bold, plain, italic
base_family = "serif", # 字体格式,可选 serif, sans, mono, Arial等
base_size = 16, # 图形的字体大小
base_line_size = 0.8, # 坐标轴的粗细
axis_text_angle = 45)+ # 可选值有 0,45,90,270
scale_fill_prism(palette = "flames")+
geom_signif(comparisons = list(c("A_1","A_2"),#显著性
c("B_1","B_2"),
c("C_1","C_2")),# 设置需要比较的组
map_signif_level = T, #是否使用星号显示
test = "t.test", ##计算方法
tip_length = c(c(0.01,0.01),
c(0.01,0.01),
c(0.01,0.01)),#横线下方的竖线设置
size=0.8,color="black")
p
image.png
网友评论