文章參考:
實戰
1.加載數據:
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
options("repos"=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
rm(list = ls()) ## 魔幻操作,一鍵清空~
options(stringsAsFactors = F)
load('GSE9257_lasso.Rdata')
###提取IL13RA2, CDH3 and COMP
load("myoverlap.Rdata")
rt=as.data.frame(t(exprSet[myoverlap,]))
rt$group=ifelse(meta_GSE9257_lasso$event==1,"IPF","Control")
x=colnames(rt)[4]
2.數據準備:
###加載需要的包
library(reshape2)
library(tidyr)
library(ggplot2)
library(ggpubr)
###數據轉換:
#把數據轉換成gglpot2輸入文件
data=melt(rt,id.vars=c("group")) ##寬數據變長數據
colnames(data)=c("group","Gene","Expression")
head(data)
3.做圖
###繪制圖形
#繪制boxplot
p=ggboxplot(data, x="Gene", y="Expression", color = "group",
ylab="Gene expression",
xlab="",
legend.title=x,
palette = c("blue","red"),
width=0.6, add = "none")
p=p+rotate_x_text(60)
p1=p+stat_compare_means(aes(group=group),
method="t.test",
symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", " ")),
label = "p.signif")
#輸出圖片
#pdf(file=outFile, width=6, height=5)
print(p1)
小提琴圖:
# plot
ggplot(data,aes(x = Gene, y = Expression,fill = group)) +
# 小提琴圖層
geom_violin(position = position_dodge(0.9),alpha = 0.5,
width = 1.2,trim = T,
color = NA) +
# 箱線圖圖層
geom_boxplot(width = .2,show.legend = F,
position = position_dodge(0.9),
color = 'grey20',alpha = 0.5,
outlier.color = 'grey50') +
# 主題調整
theme_bw(base_size = 14) +
theme(axis.text.x = element_text(angle = 0,color = 'black'),#,hjust = 1
legend.position = 'top',
aspect.ratio = 0.8) + ##圖形長寬比例
# 顏色設置
scale_fill_manual(values = c('Control'='#398AB9','IPF'='red'),
name = '') +
# 添加顯著性標記
stat_compare_means(aes(group=group),
method="t.test",
symnum.args=list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", "NS")),label = "p.signif",
label.y = 8,size = 5) +
#ylim(0.5,3.5)
###添加標題
labs(x="GSE9257")+
theme(axis.title.x = element_text(color="black", size=12, face="bold")
)
###添加標題:
p +labs(title="Plot of length \n by dose",
x ="Dose (mg)", y = "Teeth length")+
theme(
plot.title = element_text(color="red", size=12, face="bold.italic"),
axis.title.x = element_text(color="blue", size=12, face="bold"),
axis.title.y = element_text(color="#993333", size=12, face="bold")
)