美文网首页
ggplot2几种常见的散点图

ggplot2几种常见的散点图

作者: bioYIYI | 来源:发表于2022-04-12 15:34 被阅读0次

    1,散点图

    结果示例:


    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,散点图+拟合曲线+折线图

    结果示例:


    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,散点图+拟合曲线+部分点标注

    结果示例:


    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()
    

    相关文章

      网友评论

          本文标题:ggplot2几种常见的散点图

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