R语言——小提琴图 - - 科研猫

先贴代码

# Violin #

# define the data file folder here

data.file = "demo.txt" #<-- define your data file here

violin.color = "random" #<-- define the color of box

                        # use "random" to draw multiple colors

x.axis.label = "Cancer type" #<-- define the label of x axis

y.axis.label = "Expression value" #<-- define the label of x axis

options(stringsAsFactors = F)

options(warn = -1)

# Check R packages required here

if(!requireNamespace("RColorBrewer")){

  install.packages("RColorBrewer")

}

library(RColorBrewer)

if(!requireNamespace("ggplot2")){

  install.packages("ggplot2")

}

library(ggplot2)

#read in the data file

data = read.table(data.file, sep="\t", header=T)

colnames(data) = c("Value","Group")

#draw violin plots

if(toupper(violin.color) == "RANDOM"){

  ggplot(data,aes(Group,Value,fill=Group))+

    geom_violin(adjust=1,trim=T)+

    geom_boxplot(width=0.3,fill="white",alpha=0.8,outlier.colour=NA)+

    stat_summary(fun.y=mean,geom="point",fill='white',shape=21,size=2)+

    theme_bw()+

    theme(axis.text.x=element_text(angle=60,hjust=1,size=10))+

    xlab(x.axis.label)+

    ylab(y.axis.label)+

    guides(fill=F)

}else{

  ggplot(data,aes(Group,Value))+

    geom_violin(adjust=1,trim=T,fill=violin.color)+

    geom_boxplot(width=0.3,fill="white",alpha=0.8,outlier.colour=NA)+

    stat_summary(fun.y=mean,geom="point",fill='white',shape=21,size=2)+

    theme_bw()+

    theme(axis.text.x=element_text(angle=60,hjust=1,size=10))+

    xlab(x.axis.label)+

    ylab(y.axis.label)+

    guides(fill=F)

}



# 数据示例 #

12.53248119 AML

13.03209628 AML

12.46783029 AML

12.82908844 AML

11.39974676 AC

11.09084847 AC

11.51240794 AC

11.55964911 AC

11.77063751 AC

10.78955345 AC

11.68130246 AC

11.80625844 AC

11.31851773 AC

11.73438553 BUC

11.8051308 BUC

12.16311259 BUC

11.67048307 BUC

12.0448187 BUC

你可能感兴趣的:(R语言——小提琴图 - - 科研猫)