如何在R中的箱线图中添加连接点的线?

Posted

技术标签:

【中文标题】如何在R中的箱线图中添加连接点的线?【英文标题】:How to add lines connecting dots in boxplot in R? 【发布时间】:2021-08-22 23:15:07 【问题描述】:

我正在尝试制作与此类似的图表,但是当我尝试添加一条连接前后物种的线时,每个物种只出现一条垂直线。我添加了“对”列来识别每个物种之前和之后的每个点。 谁能确定我的脚本中的错误在哪里?谢谢!

structure(list(species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("1diphylla", "2calycioides", "3duckeana"
), class = "factor"), ID = c("p20", "p15", "p23", "p24", "p25", 
"p26", "p3", "p5", "p7", "p8", "p9", "p10", "p11", "p14", "p13", 
"p58", "p59", "p42", "p60", "p43", "p46", "p57", "p47", "p55", 
"p31", "p34", "p33", "p41", "p38", "p39", "p90", "p83", "p65", 
"p76", "p61", "p62", "p78", "p70", "p85", "p82", "p87", "p88", 
"p89", "p63", "p79", "p15", "p20", "p23", "p24", "p25", "p26", 
"p3", "p5", "p7", "p8", "p9", "p10", "p11", "p13", "p14", "p34", 
"p33", "p41", "p38", "p39", "p58", "p59", "p42", "p60", "p43", 
"p46", "p57", "p47", "p55", "p31", "p82", "p85", "p65", "p76", 
"p61", "p62", "p78", "p70", "p63", "p79", "p83", "p90", "p87", 
"p88", "p89"), trat = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Controle", "Dano"), class = "factor"), 
    pair = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
    13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
    25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 
    37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
    17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
    29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 
    41L, 42L, 43L, 44L, 45L), vol_p_ul = c(0.15, 0.13, 0, 0, 
    0.05, 0.3, 0.52, 0.19, 0.21, 0.02, 0.07, 0.11, 0, 0, 0.06, 
    0, 1.79, 0.21, 1.99, 3.23, 0.32, 0, 0.03, 3.94, 2.41, 5.46, 
    3.35, 1.83, 2.09, 4.75, 1.1, 0.31, 0.42, 1.43, 1.08, 0.24, 
    0.14, 1.23, 5.25, 0.67, 2.46, 0.35, 0.69, 0.49, 0.15, 0.11, 
    0.09, 0.07, 0, 0, 0, 0.13, 0, 0.42, 0.09, 0.03, 0.02, 0.07, 
    0.03, 0.05, 0, 2.33, 1.25, 0.69, 0.67, 0.13, 0.53, 0.02, 
    0.37, 1.78, 0.3, 0.02, 0.3, 0.74, 0.33, 0.59, 0.57, 0, 0.32, 
    0.51, 0.14, 0, 1.42, 0.2, 0.14, 0.38, 0, 0.36, 0, 0.24), 
    vol_f_ul = c(0.010714286, 0.01, 0, 0, 0.002173913, 0.015789474, 
    0.086666667, 0.027142857, 0.023333333, 0.002222222, 0.01, 
    0.008461538, 0, 0, 0.008571429, 0, 0.298333333, 0.019090909, 
    0.284285714, 0.538333333, 0.08, 0, 0.004285714, 0.4925, 0.241, 
    0.78, 0.67, 0.1525, 0.139333333, 0.527777778, 0.275, 0.062, 
    0.14, 0.089375, 0.063529412, 0.015, 0.01, 0.136666667, 0.22826087, 
    0.134, 0.091111111, 0.029166667, 0.115, 0.040833333, 0.011538462, 
    0.008461538, 0.006428571, 0.01, 0, 0, 0, 0.021666667, 0, 
    0.046666667, 0.01, 0.004285714, 0.001538462, 0.0175, 0.002, 
    0.007142857, 0, 0.466, 0.104166667, 0.046, 0.074444444, 0.010833333, 
    0.088333333, 0.001818182, 0.052857143, 0.296666667, 0.075, 
    0.003333333, 0.042857143, 0.0925, 0.033, 0.118, 0.024782609, 
    0, 0.02, 0.03, 0.00875, 0, 0.157777778, 0.016666667, 0.010769231, 
    0.076, 0, 0.013333333, 0, 0.04), brix = c(2.34, 5.34, NA, 
    NA, NA, 4.34, 2.34, 2.34, 3.84, NA, 8.84, 5.34, NA, NA, 11.34, 
    NA, 5.42, 3.42, 4.42, 4.92, 5.42, NA, NA, 7.42, 7.42, 6.42, 
    5.42, 4.42, 5.42, 5.42, 3.84, 3.84, 2.84, 4.34, 5.34, 4.34, 
    6.84, 8.84, 5.34, 3.34, 4.84, 5.34, 4.84, 7.34, 7.84, NA, 
    1.84, NA, NA, NA, NA, 1.84, NA, 2.84, 1.84, NA, NA, 2.84, 
    NA, NA, NA, 4.92, 6.42, 4.92, 4.42, NA, 3.42, NA, 4.42, 4.42, 
    3.92, NA, 2.42, 5.42, 1.92, 3.34, 10.34, NA, 2.34, 3.84, 
    3.34, NA, 5.34, 2.34, 4.84, 2.34, NA, 4.84, NA, 5.84), mg_acucar = c(0.024506123, 
    0.053963963, NA, NA, NA, 0.044027683, 0.024506123, 0.024506123, 
    0.039103418, NA, 0.089662318, 0.053963963, NA, NA, 0.116038643, 
    NA, 0.054763919, 0.034989639, 0.044818279, 0.049776474, 0.054763919, 
    NA, NA, 0.075006199, 0.075006199, 0.064826559, 0.054763919, 
    0.044818279, 0.054763919, 0.054763919, 0.039103418, 0.039103418, 
    0.029342638, 0.044027683, 0.053963963, 0.044027683, 0.069087758, 
    0.089662318, 0.053963963, 0.034208403, 0.048981198, 0.053963963, 
    0.048981198, 0.074187523, 0.079316538, NA, 0.019698858, NA, 
    NA, NA, NA, 0.019698858, NA, 0.029342638, 0.019698858, NA, 
    NA, 0.029342638, NA, NA, NA, 0.049776474, 0.064826559, 0.049776474, 
    0.044818279, NA, 0.034989639, NA, 0.044818279, 0.044818279, 
    0.039889334, NA, 0.025277999, 0.054763919, 0.020466054, 0.034208403, 
    0.105400363, NA, 0.024506123, 0.039103418, 0.034208403, NA, 
    0.053963963, 0.024506123, 0.048981198, 0.024506123, NA, 0.048981198, 
    NA, 0.058975978), mg_totais_p = c(0.003675918, 0.007015315, 
    NA, NA, NA, 0.013208305, 0.012743184, 0.004656163, 0.008211718, 
    NA, 0.006276362, 0.005936036, NA, NA, 0.006962319, NA, 0.098027416, 
    0.007347824, 0.089188376, 0.160778012, 0.017524454, NA, NA, 
    0.295524426, 0.180764941, 0.353953014, 0.18345913, 0.082017451, 
    0.114456592, 0.260128617, 0.043013759, 0.012122059, 0.012323908, 
    0.062959586, 0.05828108, 0.010566644, 0.009672286, 0.110284651, 
    0.283310804, 0.02291963, 0.120493746, 0.018887387, 0.033797026, 
    0.036351886, 0.011897481, NA, 0.001772897, NA, NA, NA, NA, 
    0.002560851, NA, 0.012323908, 0.001772897, NA, NA, 0.002053985, 
    NA, NA, NA, 0.115979185, 0.081033199, 0.034345767, 0.030028247, 
    NA, 0.018544509, NA, 0.016582763, 0.079776537, 0.0119668, 
    NA, 0.0075834, 0.0405253, 0.006753798, 0.020182958, 0.060078207, 
    NA, 0.007841959, 0.019942743, 0.004789176, NA, 0.076628827, 
    0.004901225, 0.006857368, 0.009312327, NA, 0.017633231, NA, 
    0.014154235), mg_totais_f = c(0.000262566, 0.00053964, NA, 
    NA, NA, 0.000695174, 0.002123864, 0.000665166, 0.000912413, 
    0.000896623, 0.000456618, NA, NA, NA, 0.000994617, NA, 0.016337903, 
    0.000667984, 0.012741197, 0.026796335, 0.004381114, NA, NA, 
    0.036940553, 0.018076494, 0.050564716, 0.036691826, 0.006834788, 
    0.007630439, 0.02890318, 0.01075344, 0.002424412, 0.004107969, 
    0.003934974, 0.003428299, 0.000660415, 0.000690878, 0.01225385, 
    0.012317861, 0.004583926, 0.004462731, 0.001573949, 0.005632838, 
    0.003029324, 0.000915191, NA, 0.000126636, NA, NA, NA, NA, 
    0.000426809, NA, 0.001369323, 0.000196989, NA, NA, 0.000513496, 
    NA, NA, NA, 0.023195837, 0.006752767, 0.002289718, 0.003336472, 
    NA, 0.003090751, NA, 0.002368966, 0.01329609, 0.0029917, 
    NA, 0.001083343, 0.005065663, 0.00067538, 0.004036592, 0.002612096, 
    NA, 0.000490122, 0.001173103, 0.000299324, NA, 0.008514314, 
    0.000408435, 0.00052749, 0.001862465, NA, 0.000653083, NA, 
    0.002359039)), row.names = c(NA, -90L), class = "data.frame")

ggplot(dat,aes(x= species, y= vol_p_ul, fill=trat)) +
  geom_boxplot() +
  geom_point(aes(fill=trat),position=position_jitterdodge(0.2), alpha=0.8)+ 
geom_line(aes(group = pair), alpha = 0.6, colour = "black")+
theme_bw() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(color="black", size=14),
axis.text.y = element_text(size = 14, color="black")) +
theme(axis.title = element_text(size = 14))

【问题讨论】:

【参考方案1】:

我同意其他回复使用facet_wrapgeom_line,但我的看法略有不同:

geom_point 几何图形是必需的,否则您只会得到异常值的点 在geom_boxplot 之后添加geom_line 和geom_point,这样它们就不会被覆盖 像示例那样抖动点的位置
ggplot(data = dat, aes(x = trat, y = vol_p_ul, fill = trat)) +
  geom_boxplot() + 
  geom_line(aes(group = pair), position=position_dodge(0.2)) +
  geom_point(aes(fill=trat,group=pair), position = position_dodge(0.2)) +
  facet_wrap(~species) 

【讨论】:

【参考方案2】:

你在寻找这样的东西吗?:

ggplot(data = dat, aes(x = trat, y = vol_p_ul)) + 
  geom_line( aes(group = pair)) +
  geom_boxplot(aes(fill = trat)) + 
  facet_wrap(~species) + 
  theme_bw() + theme(panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank()) +
  theme(axis.text.x = element_text(color="black", size=14),
        axis.title.x = element_blank(),
        axis.text.y = element_text(size = 14, color="black")) +
  theme(axis.title = element_text(size = 14))

【讨论】:

以上是关于如何在R中的箱线图中添加连接点的线?的主要内容,如果未能解决你的问题,请参考以下文章

如何在r中的箱线图之间创建单独的线图

R中的箱线图显示平均值

如何用R中的样本组制作特定行的箱线图

如何按中值对熊猫中的箱线图进行排序?

使用多个连接的箱线图更改 Matplotlib 中的轴刻度

R中多个变量的箱线图/直方图