代码之家  ›  专栏  ›  技术社区  ›  stephr

如何在ggplot2折线图的每个方面显示每组的错误条?

  •  0
  • stephr  · 技术社区  · 1 年前

    我有一个用ggplot2制作的多面折线图。在每个面内,有3条线(用于3组)。我想在每个方面显示每组的错误条。然而,我的代码只显示了1组错误条。

    以下是数据:

    
    
    
    structure(list(Ceramide = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 
    13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L), levels = c("Cer.d18.0.16.0.", "Cer.d18.0.24.0.", "Cer.d18.1.17.0.", 
    "GlcCer..d18.1.16.0.", "GlcCer..d18.1.22.0.", "GlcCer..d18.1.24.0.", 
    "SM.d18.0.16.0.", "SM.d18.0.20.0.", "SM.d18.0.24.0.", "SM.d18.1.16.0.", 
    "SM.d18.1.18.0.", "SM.d18.1.20.0.", "SM.d18.1.22.0.", "SM.d18.1.24.0."
    ), class = "factor"), Time = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 
    1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 
    2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 
    3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 
    1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 
    2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 
    3, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3), group = structure(c("control", 
    "nonsurgery", "surgery", "control", "nonsurgery", "surgery", 
    "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery", "control", "nonsurgery", "surgery", "control", "nonsurgery", 
    "surgery"), levels = c("Control", "Non-Surgery", "Surgery")), 
        lipid_median = c(31.7097370252311, 34.8841161859164, 33.8290414320265, 
        34.1684491300412, 36.8599396212937, 34.6874485703282, 32.3304714410658, 
        34.3821632026185, 34.2918789867913, 145.421759786139, 163.919371708403, 
        161.414926600578, 168.052324203937, 171.299868576961, 163.482362347776, 
        156.007891775535, 150.109050938528, 140.413393151251, 7.55427982744622, 
        8.43892179554658, 8.84364339049424, 8.47618281296365, 9.73184893405914, 
        8.89943502138335, 9.54992271330453, 9.75591139150991, 8.33992912914349, 
        120.522851205812, 125.88352056183, 124.742784442186, 135.854812388421, 
        147.013486685589, 138.741281304854, 175.417239189346, 174.897434598814, 
        187.270501509993, 677.051591528187, 713.879764664933, 736.210940972631, 
        750.318094316966, 908.009357805281, 891.9355482138, 998.724623171574, 
        1049.42761667314, 1077.18803324428, 515.501136153635, 547.04680741506, 
        573.986096454809, 606.695091894266, 705.473041362558, 696.376555407304, 
        865.287364243197, 934.24691964498, 1041.58738466609, 2396.61854413292, 
        2537.68682151989, 2655.14871236125, 2709.59299639615, 2836.2032465437, 
        2740.33497762081, 2891.72482041317, 3120.80299514295, 3398.57801419228, 
        1192.04543834071, 1325.15708062365, 1373.82781244999, 1261.73343893034, 
        1493.43715612378, 1378.26319014475, 1308.33341650225, 1427.098659029, 
        1383.38634626637, 225.48902982964, 255.589558412548, 260.787456931915, 
        239.491154054235, 264.145991461667, 258.183045212106, 243.190264905069, 
        257.541556100897, 245.264138149509, 44846.5182860006, 46612.102474005, 
        49407.1947133826, 46418.2518567308, 50920.7013400283, 50964.4113345973, 
        47738.1122862251, 50623.1346625701, 53886.4226383792, 10121.4255466185, 
        10560.678173424, 11812.8685609723, 10248.8308646972, 11459.0944364258, 
        10906.0994579433, 10056.9557976663, 10349.2740180237, 9543.47413288215, 
        22840.2447470509, 24353.1897501268, 26494.3889699438, 23295.5202105924, 
        29569.7631259859, 26427.4951377757, 28332.8398285893, 29242.663675407, 
        28274.2067377423, 48590.5678321935, 57003.9794389598, 61764.7881690795, 
        52978.3341523694, 64576.4593418158, 58564.0252854514, 58321.7642765169, 
        62054.6822882946, 58373.9112859722, 21062.0272182675, 23075.0210421309, 
        25978.4554297391, 23613.9666659656, 28988.7090450242, 26758.4962229301, 
        26935.1615084363, 28585.1403999489, 28525.4576752008), error25_mean = c(24.2346184329791, 
        24.2346184329791, 24.2346184329791, 25.5670968115505, 25.5670968115505, 
        25.5670968115505, 25.096215055222, 25.096215055222, 25.096215055222, 
        105.383596099945, 105.383596099945, 105.383596099945, 111.642319476418, 
        111.642319476418, 111.642319476418, 100.924841961843, 100.924841961843, 
        100.924841961843, 4.69249765133767, 4.69249765133767, 4.69249765133767, 
        5.53075253153509, 5.53075253153509, 5.53075253153509, 5.44700156122991, 
        5.44700156122991, 5.44700156122991, 90.5082978297349, 90.5082978297349, 
        90.5082978297349, 102.435152667828, 102.435152667828, 102.435152667828, 
        128.990824500558, 128.990824500558, 128.990824500558, 366.618976742406, 
        366.618976742406, 366.618976742406, 515.694055014973, 515.694055014973, 
        515.694055014973, 584.752767225474, 584.752767225474, 584.752767225474, 
        297.695004557019, 297.695004557019, 297.695004557019, 417.989154819201, 
        417.989154819201, 417.989154819201, 533.575947699868, 533.575947699868, 
        533.575947699868, 1809.98995236381, 1809.98995236381, 1809.98995236381, 
        2184.20172139014, 2184.20172139014, 2184.20172139014, 2282.69728959617, 
        2282.69728959617, 2282.69728959617, 627.061827023858, 627.061827023858, 
        627.061827023858, 792.813146312815, 792.813146312815, 792.813146312815, 
        758.242434200142, 758.242434200142, 758.242434200142, 125.307690779964, 
        125.307690779964, 125.307690779964, 146.508496378177, 146.508496378177, 
        146.508496378177, 144.668297639273, 144.668297639273, 144.668297639273, 
        33773.788835109, 33773.788835109, 33773.788835109, 39348.7616985581, 
        39348.7616985581, 39348.7616985581, 37887.3127288562, 37887.3127288562, 
        37887.3127288562, 7665.54019614451, 7665.54019614451, 7665.54019614451, 
        8343.47012458038, 8343.47012458038, 8343.47012458038, 7632.22950854432, 
        7632.22950854432, 7632.22950854432, 12817.5016527568, 12817.5016527568, 
        12817.5016527568, 16840.1422237024, 16840.1422237024, 16840.1422237024, 
        16712.3475713006, 16712.3475713006, 16712.3475713006, 27344.676729014, 
        27344.676729014, 27344.676729014, 33688.9651052161, 33688.9651052161, 
        33688.9651052161, 30563.0891504596, 30563.0891504596, 30563.0891504596, 
        11222.4607168074, 11222.4607168074, 11222.4607168074, 14474.5924596798, 
        14474.5924596798, 14474.5924596798, 14996.2737491839, 14996.2737491839, 
        14996.2737491839), error75_mean = c(45.6274436242186, 45.6274436242186, 
        45.6274436242186, 46.3250271711141, 46.3250271711141, 46.3250271711141, 
        44.1410863901294, 44.1410863901294, 44.1410863901294, 257.298206727319, 
        257.298206727319, 257.298206727319, 236.928413792139, 236.928413792139, 
        236.928413792139, 222.166200366427, 222.166200366427, 222.166200366427, 
        13.9818618325458, 13.9818618325458, 13.9818618325458, 14.737541922349, 
        14.737541922349, 14.737541922349, 14.681766170529, 14.681766170529, 
        14.681766170529, 171.639325994704, 171.639325994704, 171.639325994704, 
        192.425598936517, 192.425598936517, 192.425598936517, 235.054195358118, 
        235.054195358118, 235.054195358118, 1345.92244450824, 1345.92244450824, 
        1345.92244450824, 1446.37417417621, 1446.37417417621, 1446.37417417621, 
        1690.12813085014, 1690.12813085014, 1690.12813085014, 988.599076943615, 
        988.599076943615, 988.599076943615, 1124.21640197936, 1124.21640197936, 
        1124.21640197936, 1421.89923746621, 1421.89923746621, 1421.89923746621, 
        3583.04960825947, 3583.04960825947, 3583.04960825947, 3565.4733498257, 
        3565.4733498257, 3565.4733498257, 4101.06275926889, 4101.06275926889, 
        4101.06275926889, 2612.34329875199, 2612.34329875199, 2612.34329875199, 
        2433.16211146142, 2433.16211146142, 2433.16211146142, 2263.42116616642, 
        2263.42116616642, 2263.42116616642, 461.090471271548, 461.090471271548, 
        461.090471271548, 442.036576511789, 442.036576511789, 442.036576511789, 
        413.885341711507, 413.885341711507, 413.885341711507, 64516.9309659138, 
        64516.9309659138, 64516.9309659138, 63219.8911218361, 63219.8911218361, 
        63219.8911218361, 65114.6601617267, 65114.6601617267, 65114.6601617267, 
        15112.1061771317, 15112.1061771317, 15112.1061771317, 14380.35473491, 
        14380.35473491, 14380.35473491, 12917.5242138469, 12917.5242138469, 
        12917.5242138469, 40726.4653152597, 40726.4653152597, 40726.4653152597, 
        42029.1504387542, 42029.1504387542, 42029.1504387542, 44774.7182898133, 
        44774.7182898133, 44774.7182898133, 100670.488661911, 100670.488661911, 
        100670.488661911, 98911.2606696033, 98911.2606696033, 98911.2606696033, 
        96209.4844634763, 96209.4844634763, 96209.4844634763, 49892.4612000841, 
        49892.4612000841, 49892.4612000841, 49214.3230551524, 49214.3230551524, 
        49214.3230551524, 49239.3503559168, 49239.3503559168, 49239.3503559168
        )), row.names = c(NA, -126L), class = c("tbl_df", "tbl", 
    "data.frame"))
    

    这是我的代码:

    
    # Define custom labels for Ceramide names to use in the graph
    ceramide_labels <- c("Cer.d18.0.16.0." = "Cer(d18:0/16:0)",
                         "Cer.d18.0.24.0." = "Cer(d18:0/24:0)",
                         "Cer.d18.1.17.0." = "Cer(d18:1/17:0)",
                         "GlcCer..d18.1.16.0." = "GlcCer (d18:1/16:0)",
                         "GlcCer..d18.1.22.0." = "GlcCer (d18:1/22:0)",
                         "GlcCer..d18.1.24.0." = "GlcCer (d18:1/24:0)",
                         "SM.d18.0.16.0."="SM(d18:0/16:0)",
                         "SM.d18.0.20.0." = "SM(d18:0/20:0)",
                         "SM.d18.0.24.0."= "SM(d18:0/24:0)",
                         "SM.d18.1.16.0." = "SM(d18:1/16:0)",
                         "SM.d18.1.18.0." = "SM(d18:1/18:0)",
                         "SM.d18.1.20.0." = "SM(d18:1/20:0)",
                         "SM.d18.1.22.0." = "SM(d18:1/22:0)",
                         "SM.d18.1.24.0." = "SM(d18:1/24:0)")
    
    # Creating a multifaceted line plot of the nominally significant ceramides
    plot<-graphs%>%
      ggplot(aes(x = Time, y = lipid_median)) +
      geom_line(aes(color = group, group = group), linewidth = 0.5) + # Set line width
      geom_point(aes(shape = group), size = 2.5) + # Set point color
      facet_wrap(~ Ceramide, scales = "free_y", labeller = labeller(Ceramide = ceramide_labels), 
                  nrow = 4) + # Adjust the height and width as needed) +
      geom_errorbar(aes(ymin = error25_mean, ymax = error75_mean, color=group), width = 0.2) +  # Add error bars
      labs(x = "Time Point", 
           y = "Sphingolipid Value", 
           title = "Nominally Significant Sphingolipid Means over Time",
           color = "Group") +
      theme_minimal() +
      guides(color = "none", shape = guide_legend(title = "Group")) +  
      scale_x_continuous(breaks = c(1, 2, 3), labels = c("0", "2 Years", "12 Years")) +
      scale_y_continuous(expand = c(0, 0)) +
      theme(plot.margin = margin(20, 30, 20, 30, "pt"), 
            panel.spacing = unit(0.5, "lines"),  # Adjust panel spacing
            #axis.ticks.x = element_blank(),  # Remove x-axis ticks
            axis.text.x = element_text(color = "black", size = 6.5),  # Keep x-axis labels
            strip.text = element_text(size = 8),
            strip.background = element_rect(fill='beige'),  
            panel.background = element_rect(fill = 'white',color='black'),
            plot.background = element_rect(fill = 'white', color = 'white'),
            panel.border =element_rect(colour='black', fill=NA),
            plot.title = element_text(hjust = 0.5)) #+  # Center the plot title
    

    我如何才能显示所有3组的错误条?我希望误差条与不同组的线条颜色相匹配

    1 回复  |  直到 1 年前
        1
  •  0
  •   Edward    1 年前

    你需要 闪避 误差条,可能还有点。

    graphs %>%
      ggplot(aes(x = Time, y = lipid_median)) +
      geom_line(aes(color = group, group = group), linewidth = 0.5) + # Set line width
      geom_point(aes(shape = group), size = 2.5,
                 position = position_dodge(width=0.2)) + # Set point color
      facet_wrap(~ Ceramide, scales = "free_y", labeller = labeller(Ceramide = ceramide_labels), 
                 nrow = 4) + # Adjust the height and width as needed) +
      geom_errorbar(aes(ymin = error25_mean, ymax = error75_mean, color=group), 
                    width = 0.2,
                    position = position_dodge(width=0.2)) + ... # Add error bars
    

    enter image description here