ggplot2幾種常見的散點(diǎn)圖

1,散點(diǎn)圖

結(jié)果示例:


image.png

輸入文件示例:


image.png

代碼:

library(ggplot2)
data<-read.table("ch_num_per_sample.stat.add_age.cancer",head=T,sep='\t')
p<-cor.test(data$CH.number,data$Age,method="spearman")
ggplot(data) + geom_point(aes(Age, CH.number), alpha=0.25, shape=16,stat = "identity",position = "identity") + xlim(0, 120) + ylim(0, 35) + coord_fixed() + xlab('Age') + ylab('CH number')+annotate(geom="text",x=91,y=32.5,label=paste('Spearman Correlation ( corr=',round(p$estimate[[1]],4),';p=',p$p.value,' )',sep=""),colour='black',size=2.3)
ggsave('Age_CH.number_point.png',dpi = 1080)

2,散點(diǎn)圖+擬合曲線+折線圖

結(jié)果示例:


image.png

輸入文件示例:


image.png

代碼:

library(ggplot2)
data<-read.table("age_stat.age.each.cancer.xls",header=T,sep="\t")
pdf("CH_age_cor.pdf", width=12, height=6)
ggplot(data,aes(age, Percent)) + geom_point( alpha=0.25, shape=16,stat = "identity",position = "identity") + xlab('Age') + ylab('Percent (%)') +stat_smooth(method="lm") +geom_line(aes(age, Percent,group=Cancer,color=Cancer),alpha=0.55)+scale_x_continuous(breaks=c(1,2,3,4,5,6,7),labels=c("<=40","40-50","50-60","60-70","70-80","80-90","90-100"))
dev.off()

3,散點(diǎn)圖+擬合曲線+部分點(diǎn)標(biāo)注

結(jié)果示例:


image.png

輸入文件示例:


image.png

代碼:

library(plyr)
library(ggplot2)
library(ggrepel)
data<-read.table("carry_ratio.stat.xls",header=T,sep="\t",row.names=1)
p<-cor.test(data$tissue_ratio,data$plasma_ratio)
sub1<-data[data$tissue_ratio>data$plasma_ratio,]
sub2<-data[data$tissue_ratio<data$plasma_ratio,]
pdf("Prevalence_Correlation.pdf", width=6, height=6)
ggplot(data,aes(tissue_ratio, plasma_ratio)) + geom_point(alpha=0.8, shape=16,stat = "identity",position = "identity") + coord_fixed() + xlab('Tissue prevalence (%)') + ylab('Plasma prevalence (%)') + stat_smooth(method="lm") + annotate(geom="text",x=20,y=80,label=paste('Pearson\'s Correlation ( R^2=',round(p$estimate[[1]],3),'; p=',round(p$p.value,5),' )',sep=""),colour='black',size=3) + theme(axis.title.y =element_text(size=14),axis.text.y=element_text(size=12)) +  theme(axis.title.x =element_text(size=14),axis.text.x =element_text(size=12))+geom_text_repel(data=sub1,aes( tissue_ratio, plasma_ratio, label = rownames(sub1)),segment.size = 0.1,size=2,col='red') +  geom_text_repel(data=sub2,aes(tissue_ratio, plasma_ratio, label = rownames(sub2)),segment.size = 0.1,size=1.8,col='blue')+ theme_classic(base_size = 13)
dev.off()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容