美文网首页
R 如何绘制小提琴图

R 如何绘制小提琴图

作者: 小qqq | 来源:发表于2022-06-27 13:35 被阅读0次

准备数据文件.txt

这里准备的是BMI.txt(依旧是自己准备的数据)


image.png

绘制小提琴图:

BMI=read.table("BMI.txt",header = T,row.names=1,sep="\t")
library(ggplot2)

BMI$name=rownames(BMI)
BMI=BMI[,-6]
BMI
library(reshape2)
library(ggpubr)
library(ggsignif)
data=melt(BMI,id="gender")
ggviolin(data,x="variable",y="value",fill="gender",
         palette=c('lancet'))#调色palette
image.png

小提琴图添加箱线图:

#添加箱线图
ggviolin(data,x="variable",y="value",fill="gender",
         palette=c('lancet'),
         add="boxplot",
         add.params = list(fill="white"))

############################################
BMI=read.table("BMI.txt",header = T,row.names=1,sep="\t")
library(ggplot2)

BMI$name=rownames(BMI)
BMI=BMI[,-6]
BMI
library(reshape2)
library(ggpubr)
library(ggsignif)
data=melt(BMI,id="gender")
library(ggplot2)
library(ggsci)
#install.packages("cowplot")
library(cowplot)
p<-ggplot(data,aes(x=variable,y=value,fill=gender))+
  geom_violin(aes(fill=gender),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("weight","BMI")),test="t.test",
    tip_length = 0.02,size = 1,textsize = 4,#增加***尺寸
    map_signif_level = T #用*表示显著性,*---0.05,**---0.01,***---0.001
  )+
  scale_fill_brewer(palette = "Dark2")+
  theme_cowplot() + theme(legend.position = c(0.85, 0.2))+
  theme_classic()+
  ggtitle("boxplot")+
  labs(title = 'boxplot', # 添加主标题
       x = "variable", # 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)) # 图四周框起来
pdf("violin.pdf",width = 12,height = 8)
ggarrange(p, ncol = 2, nrow = 2)
dev.off()
image.png

由于没有合适的数据,会出来的图有些许怪,主要关注绘图的方法~

相关文章

网友评论

      本文标题:R 如何绘制小提琴图

      本文链接:https://www.haomeiwen.com/subject/cwbavrtx.html