将因子水平添加到 ggplot 中的 ggpmisc::stat_poly_eq() 标签

问题描述 投票:0回答:1

我有不同因子水平的数据,我正在拟合线性回归并在 ggplot 中绘制方程和 r2 。我想将相应的因素水平添加到这些水平中。有办法做到这一点吗?我知道我可以在方程前添加一个字符串

eq.with.lhs
,但我无法调整它以动态使用相应的级别。

require(ggplot2)
require(ggpmisc)

ggplot(dat, aes(x=x, y=y, color=group)) +
  geom_point() +
  stat_smooth(data=dat, method = "glm", formula = y~x, size = 1, se=F) +
  ggpmisc::stat_poly_eq(formula = y~x, 
                        eq.with.lhs = 'group:~',
                        aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
                        parse = TRUE, size=5,
                        position = position_dodge(),
                        label.x.npc = "right") 

Ggplot

编辑:我找到了

grp.label
参数,但它没有使用实际的因子标签。

ggplot(dat, aes(x=x, y=y, color=group)) +
  geom_point() +
  stat_smooth(data=dat, method = "glm", formula = y~x, size = 1, se=F) +
  ggpmisc::stat_poly_eq(formula = y~x, 
                        aes(label = paste('group',..grp.label..,..eq.label.., ..rr.label.., sep = "~~~")), 
                        parse = TRUE, size=5,
                        position = position_dodge(),
                        label.x.npc = "right") 

Ggplot2

样本数据:

dat<-structure(list(x = c(82, 82, 82, 82, 82, 82, 81, 81, 81, 102, 
                          102, 102, 92, 92, 92, 95, 95, 95, 83, 83, 83, 115, 115, 115, 
                          93, 93, 93, 78, 78, 78, 98, 98, 98, 87, 87, 87, 79, 79, 79, 88, 
                          88, 88, 106, 106, 106, 93, 93, 93, 74, 74, 74, 91, 91, 91, 90, 
                          90, 90, 93, 93, 93, 105, 105, 105, 82, 82, 82, 79, 79, 79, 94, 
                          94, 94, 100, 100, 100, 95, 95, 95, 82, 82, 82, 90, 90, 90, 85, 
                          85, 85, 78, 78, 78, 91, 91, 91, 100, 100, 100, 84, 84, 84, 89, 
                          89, 89, 96, 96, 96, 118, 118, 118, 99, 99, 99, 94, 94, 94, 83, 
                          83, 83, 78, 78, 78, 78, 78, 78, 79, 79, 79, 93, 93, 93, 86, 86, 
                          86, 79, 79, 79, 83, 83, 83, 99, 99, 99, 115, 115, 115, 96, 96, 
                          96, 97, 97, 97, 72, 72, 72, 81, 81, 81, 102, 102, 102, 100, 100, 
                          100, 88, 88, 88, 78, 78, 78, 96, 96, 96, 88, 88, 88, 91, 91, 
                          91, 81, 81, 81, 73, 73, 73, 72, 72, 72, 98, 98, 98, 76, 76, 76, 
                          78, 78, 78, 92, 92, 92, 92, 92, 92, 101, 101, 101, 85, 85, 85, 
                          83, 83, 83), 
                    y = c(107.602729528536, 99.8776674937965, 104.424565756824, 
                                             90.8639784946237, 87.567876344086, 82.8693548387097, 90.0380376344086, 
                                             86.600940860215, 81.2975806451613, 85.9278225806452, 83.280376344086, 
                                             81.2158602150538, 89.832123655914, 82.8462365591398, 86.5051075268817, 
                                             90.447311827957, 87.0502688172043, 83.5110215053763, 86.7975806451613, 
                                             80.140188172043, 84.0159946236559, 78.9743279569892, 83.6586021505376, 
                                             86.6870967741935, 90.7225806451613, 86.7483870967742, 83.9694892473118, 
                                             92.7986559139785, 84.1493279569893, 88.4852150537634, 90.6447580645161, 
                                             86.1530913978495, 81.7055107526882, 94.8112903225806, 90.357123655914, 
                                             83.7211021505376, 87.4201612903226, 91.9633064516129, 82.0004032258065, 
                                             88.2493279569892, 84.1396505376344, 80.9931451612903, 85.5883064516129, 
                                             89.9623655913979, 81.7731182795699, 90.608064516129, 88.3956989247312, 
                                             83.2534946236559, 93.7911290322581, 89.7630376344086, 83.7809139784946, 
                                             81.7587365591398, 83.4926075268817, 87.1807795698925, 84.2247311827957, 
                                             81.924059139785, 77.1817204301075, 83.7533602150538, 81.7426075268817, 
                                             76.305376344086, 84.8920698924731, 82.5706989247312, 77.1573924731183, 
                                             84.4717741935484, 79.5698924731183, 87.6274193548387, 81.6961021505376, 
                                             84.5673387096774, 82.4397849462366, 83.4729838709677, 81.3930107526882, 
                                             80.2362903225806, 87.0844086021505, 83.6047043010753, 79.5805107526882, 
                                             90.476747311828, 86.6702956989247, 80.694623655914, 90.9407258064516, 
                                             86.7385752688172, 79.6025537634409, 78.7971774193548, 86.3478494623656, 
                                             82.8462365591398, 87.8939516129032, 79.6630376344086, 84.848252688172, 
                                             95.773252688172, 91.9741935483871, 86.1434139784946, 92.3307795698925, 
                                             88.0712365591398, 82.1233870967742, 87.2297043010753, 82.8612903225806, 
                                             79.9091397849462, 91.4573924731183, 86.798252688172, 83.9409946236559, 
                                             89.3888440860215, 84.9650537634409, 82.2715053763441, 82.7145161290323, 
                                             77.9295698924731, 85.3817204301075, 86.0987903225806, 82.7287634408602, 
                                             78.6854838709677, 89.4825268817204, 85.960752688172, 81.6052419354839, 
                                             86.9888440860215, 82.5275537634409, 81.7551075268817, 88.5900537634409, 
                                             83.9685483870968, 79.3694892473118, 88.1409946236559, 82.2690860215054, 
                                             92.7850806451613, 92.6782258064516, 88.801747311828, 83.7575268817204, 
                                             92.75, 88.077688172043, 82.6377688172043, 89.1022849462366, 85.7037634408602, 
                                             81.9489247311828, 89.234811827957, 80.5900537634409, 85.9623655913979, 
                                             86.8760752688172, 81.7173387096774, 91.9469086021505, 84.269623655914, 
                                             80.6903225806452, 78.1684139784946, 84.0158602150538, 81.8782258064516, 
                                             83.8809139784946, 83.9383064516129, 82.3279569892473, 81.8377688172043, 
                                             87.4725806451613, 88.7084677419355, 91.3086021505376, 86.3783602150538, 
                                             91.0407258064516, 86.6805107526882, 94.1415322580645, 89.4284946236559, 
                                             83.2502688172043, 91.6952956989247, 86.9588709677419, 82.1426075268817, 
                                             88.4067204301075, 85.1038978494624, 81.4271505376344, 85.9383064516129, 
                                             81.6529569892473, 82.8436827956989, 88.9259408602151, 85.242876344086, 
                                             81.9450268817204, 95.6059139784946, 90.8930107526882, 85.8310483870968, 
                                             85.5677419354839, 82.6211021505376, 79.7998655913978, 84.366935483871, 
                                             81.2752688172043, 79.682123655914, 87.0655913978495, 82.3008064516129, 
                                             82.450940860215, 83.261559139785, 91.9853494623656, 87.0489247311828, 
                                             90.7456989247312, 86.5798387096774, 85.8288978494624, 85.2977150537634, 
                                             82.2959677419355, 80.3965053763441, 86.251747311828, 82.3384408602151, 
                                             83.6129032258064, 93.6278225806452, 89.3600806451613, 85.398252688172, 
                                             89.5139784946237, 85.4555107526882, 82.1475806451613, 84.7258064516129, 
                                             82.0811827956989, 80.327688172043, 83.9979838709677, 81.8012096774194, 
                                             87.3209677419355, 90.7717741935484, 85.9436827956989, 81.4002688172043, 
                                             97.0591397849462, 91.369623655914, 84.7225806451613, 95.1681451612903, 
                                             90.0939516129032, 85.590188172043), 
                    group = structure(c(7L, 1L, 4L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 7L, 4L, 1L, 4L, 7L, 
                                        1L, 7L, 4L, 7L, 4L, 1L, 1L, 4L, 7L, 1L, 7L, 4L, 1L, 4L, 7L, 1L, 
                                        4L, 7L, 4L, 1L, 7L, 1L, 4L, 7L, 4L, 1L, 7L, 1L, 4L, 7L, 1L, 4L, 
                                        7L, 7L, 4L, 1L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 4L, 7L, 1L, 
                                        7L, 1L, 4L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 7L, 
                                        1L, 4L, 1L, 7L, 4L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 
                                        7L, 1L, 4L, 7L, 4L, 7L, 1L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 
                                        1L, 4L, 7L, 4L, 7L, 1L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 
                                        7L, 4L, 4L, 7L, 1L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 4L, 7L, 
                                        1L, 7L, 1L, 4L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 7L, 4L, 
                                        1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 7L, 
                                        1L, 4L, 1L, 7L, 4L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 
                                        7L, 1L, 4L, 7L, 4L, 7L, 1L, 1L, 4L, 7L, 1L, 4L, 7L, 1L, 4L, 7L), 
                                      levels = c("5", "6.3", "7.9", "10", "12.6", "15.8", "20", 
                                                 "25.1", "31.6", "39.8", "50.1", "63.1", "79.4", "100"), class = "factor")), 
               row.names = c(3L, 11L, 14L, 15L, 18L, 27L, 30L, 33L, 36L, 45L, 48L, 51L, 58L, 61L, 
                             70L, 78L, 81L, 84L, 85L, 90L, 91L, 102L, 103L, 109L, 116L, 119L, 
                             122L, 128L, 131L, 139L, 147L, 150L, 153L, 155L, 158L, 161L, 171L, 
                             172L, 174L, 183L, 190L, 193L, 199L, 200L, 203L, 218L, 221L, 224L, 
                             225L, 228L, 231L, 241L, 249L, 251L, 253L, 257L, 263L, 267L, 270L, 
                             273L, 281L, 284L, 287L, 296L, 299L, 306L, 309L, 317L, 320L, 324L, 
                             327L, 333L, 337L, 340L, 343L, 352L, 355L, 358L, 365L, 367L, 370L, 
                             379L, 387L, 390L, 395L, 397L, 398L, 408L, 411L, 414L, 423L, 426L, 
                             429L, 436L, 437L, 440L, 455L, 458L, 461L, 463L, 467L, 470L, 478L, 
                             479L, 487L, 491L, 498L, 501L, 505L, 508L, 509L, 523L, 526L, 529L, 
                             534L, 537L, 540L, 547L, 549L, 557L, 562L, 568L, 571L, 575L, 578L, 
                             580L, 593L, 596L, 599L, 605L, 607L, 608L, 617L, 620L, 628L, 633L, 
                             638L, 641L, 646L, 649L, 658L, 661L, 664L, 667L, 673L, 676L, 683L, 
                             687L, 695L, 698L, 701L, 705L, 711L, 715L, 718L, 721L, 730L, 733L, 
                             736L, 743L, 746L, 756L, 764L, 767L, 770L, 772L, 775L, 781L, 785L, 
                             788L, 791L, 802L, 805L, 808L, 813L, 815L, 818L, 827L, 835L, 838L, 
                             841L, 846L, 847L, 855L, 860L, 863L, 874L, 877L, 880L, 883L, 886L, 
                             895L, 900L, 903L, 906L, 912L, 915L, 918L, 925L, 927L, 935L, 939L, 
                             947L, 950L, 953L, 956L, 964L, 969L, 972L, 975L), class = "data.frame")
r ggplot2 ggpmisc
1个回答
0
投票

你就快到了。

您需要通过

grp.label
aes
传递给映射参数:

ggplot(dat, aes(x=x, y=y, color=group, grp.label=group)) +
  geom_point() +
  stat_smooth(data=dat, method = "glm", formula = y~x, size = 1, se=F) +
  ggpmisc::stat_poly_eq(formula = y~x, 
                        aes(label = paste('group', ..grp.label..,..eq.label.., ..rr.label.., sep = "~~~")), 
                        parse = TRUE, size=5,
                        position = position_dodge(),
                        label.x.npc = "right") 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.