美文网首页
R语言:ggplot添加配对连线

R语言:ggplot添加配对连线

作者: 胡童远 | 来源:发表于2021-05-27 17:51 被阅读0次

    以箱图和散点图为例,给ggplot散点添加配对连线。方法使用简单,就是geom_line(aes(group = pair))即可。给geom_point添加配对连线时需要使用position_dodge函数配置线端点位置与点一致。

    一、箱图:添加散点,连线

    1 输入格式

    id  group   Completeness
    AF04-12 BGISEQ  99.25
    AF04-12 Illumina    98.76
    AF04-17 BGISEQ  99.46
    AF04-17 Illumina    98.9
    AF04-28 BGISEQ  93.55
    AF04-28 Illumina    93.55
    AF11-25B    BGISEQ  99.54
    AF11-25B    Illumina    99.08
    

    2 wilcox分析和绘图代码

    ##  complete
    library("ggplot2")
    data = read.table("data.txt", header=T, sep="\t")
    ## wilcox
    bgi = data[data$group=="BGISEQ",]$Completeness
    illu = data[data$group=="Illumina",]$Completeness
    mean(bgi)
    mean(illu)
    wilcox.test(bgi, illu, paired = TRUE)
    
    ##
    Title = "Wilcoxon p value < 2.5e-13"
    p = ggplot(data, aes(x = group, y = Completeness, color=group)) + 
      labs(x="", y="Completeness (%)", title=Title) +
      theme_classic() +
      geom_boxplot(width=0.5, outlier.colour = NA, lwd = 1) +
      geom_line(aes(group=id), color="gray" ,position = position_dodge(0.2)) +
      geom_point(aes(fill=group, group=id), 
                 size = 2, 
                 position = position_dodge(0.2)) +
      scale_fill_manual(values = c('orangered3','deepskyblue3')) +
      theme(panel.grid = element_line(colour = 'white')) +
      theme(legend.position="none") +
      theme(axis.title = element_text(size = 30),
            axis.text = element_text(size = 28),
            axis.line = element_line(size = 1),
            axis.ticks = element_line(size = 1),
            title = element_text(size = 20))
    
    ggsave(p, filename="complete.png")
    

    二、PCOA散点图:添加散点,连线

    1 输入数据格式

      id    group Completeness Contamination    gc contigs largest  length
    1 AF04_12   BGISEQ        99.25          0.13 42.03     143  332931 5076534
    2 AF04_12 Illumina        98.76          0.13 42.00     136  365905 5105661
    3 AF04_17   BGISEQ        99.46          0.08 41.99     106  412791 6672015
    4 AF04_17 Illumina        98.90          0.08 41.98     104  386819 6663758
    5 AF04_28   BGISEQ        93.55          0.61 42.57      36  744099 4803502
    6 AF04_28 Illumina        93.55          1.99 42.54      36  616477 4874474
         N50 N_per_100_kbp Genome_fraction
    1  80303          4.14          99.061
    2  83739          9.01          99.634
    3 140135          1.05          99.899
    4 137940          4.53          99.763
    5 363431        696.22          97.450
    6 363455         13.13          99.569
    

    2 绘图数据格式

    > head(pcoa_point)
          Axis.1      Axis.2 Platform    pair
    1 -0.1485042  0.08032245   BGISEQ AF04_12
    2 -0.1490304  0.07219616 Illumina AF04_12
    3 -0.2436660  0.02873192   BGISEQ AF04_17
    4 -0.2445737  0.03555739 Illumina AF04_17
    5 -0.1109480 -0.05709881   BGISEQ AF04_28
    6 -0.1157950 -0.03534015 Illumina AF04_28
    

    3 PCOA分析,画图代码

    library(vegan)
    library(ape)
    setwd("../pcoa/")
    
    data = read.table("data.txt", sep="\t", header=T)
    input = data[, c(-1, -2)]
    
    veg = vegdist(input, method = "bray")
    pcoa = pcoa(veg)
    
    pcoa_point = data.frame(pcoa$vectors[, c(1, 2)], Platform = data$group, pair = data$id)
    #write.csv(pcoa_point, file="pcoa_bc.csv")
        
    xylab = paste(c("PCoA1: ", "PCoA2: "), round(pcoa$values$Relative_eig[1:2]*100, 2), "%", sep = "")
    
    # https://datavizpyr.com/connect-paired-points-with-lines-in-scatterplot-in-ggplot2/
    result = 
    ggplot(pcoa_point, aes(x=Axis.1, y=Axis.2, color=Platform)) +
      geom_point(pch=19, size=4) + 
      stat_ellipse(level = 0.95, show.legend = F, aes(color=Platform)) +
      geom_line(aes(group = pair), color = "grey") +
      theme_classic() +
      labs(x=xylab[1], y=xylab[2], title = paste('Bray-Curtis PCoA')) +
      theme(legend.background = element_rect(color = "black", linetype = "solid", size = 1)) +
      theme(legend.text=element_text(size=15)) +
      theme(legend.title=element_text(face='bold', size=20)) +
      #theme(text=element_text(family="serif")) +
      theme(axis.title = element_text(size = 20)) +
      theme(axis.text = element_text(size = 15),
            axis.line = element_line(size = 1),
            axis.ticks = element_line(size = 1)) +
      theme(plot.title = element_text(face="bold", size=16)) +
      scale_fill_manual(
        values = c("BGISEQ" = "orangered3",
        "Illumina" = "deepskyblue3")) +
      scale_color_manual(
        values = c("BGISEQ" = "orangered3",
        "Illumina" = "deepskyblue3"))
    
    ggsave(result, filename="pcoa_bc.png", width = 8)
    

    三、棒棒糖图

    1 输入数据格式

            id length_bgi length_illumina illumina_minus_bgi           Genus
    1  AF04-12       1524            1524                  0     Bacteroides
    2  AF04-17       1525            1525                  0     Bacteroides
    3  AF04-28        992            1525                533     Bacteroides
    4 AF11-25B       1434            1526                 92 Bifidobacterium
    5  AF13-35       1545            1545                  0   Streptococcus
    6  AF14-49       1522             984               -538   Butyricimonas
    

    2 数据处理及绘图代码

    library(ggplot2)
    df = read.table("data.txt", header=T, sep="\t")
    
    data = df[order(df$length_bgi, decreasing=T), c(1,2,3)]
    input = melt(data, id='id')
    input$id = factor(input$id, levels = c(unique(input$id)))
    
    result = 
    ggplot(input, aes(x=value, y=id)) +
    geom_line(aes(group = id)) +
    geom_point(aes(color = variable), size=4) +
    theme(legend.position="top")
    
    ggsave(result, file="test.pdf", height = 14)
    

    参考更多:
    How To Connect Paired Points with Lines in Scatterplot in ggplot2?
    https://datavizpyr.com/dumbbell-plot-in-r-with-ggplot2/

    相关文章

      网友评论

          本文标题:R语言:ggplot添加配对连线

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