Legend.labs survminer 中的参数未生成正确的名称

Legend.labs parameter in survminer does not produce the right names

我有一个乳腺癌数据集,并通过生存分析来分析数据。我正在使用 survminer 包来可视化数据。我将死亡时间作为数据集中“等级”变量的函数。等级变量有 3 个水平 (1,2,3)。我只想产生一个风险 table,但我的图例上出现了一个奇怪的 a、a、a,我无法使用 legend.labs 参数更改它。我将不胜感激任何帮助。我的代码如下:

b<-structure(list(grade = structure(c(3L, 1L, 2L, 1L, 2L, 1L, 2L, 
                               2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
                               3L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 
                               2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
                               2L, 1L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 
                               2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 
                               1L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 2L, 
                               2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 2L, 
                               2L, 2L, 2L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 
                               3L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 
                               2L, 2L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 
                               2L, 3L, 2L, 1L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 
                               2L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 2L, 
                               2L, 1L, 2L, 1L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 2L, 
                               2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
                               3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 
                               2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 
                               2L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 
                               2L, 3L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
                               2L, 2L, 2L, 3L, 1L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 
                               1L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 1L, 2L, 2L, 3L, 2L, 1L, 2L, 2L, 
                               3L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 
                               2L, 2L, 2L, 2L, 1L, 3L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                               2L, 1L, 2L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 
                               2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 
                               2L, 2L, 1L, 1L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 
                               2L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 3L, 
                               2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, 
                               1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
                               3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 3L, 2L, 2L, 1L, 
                               2L, 1L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 2L, 2L, 3L, 2L, 1L, 2L, 
                               2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 1L, 
                               2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 
                               1L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 
                               3L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 
                               2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 1L, 2L, 
                               3L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 1L, 2L, 
                               2L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
                               3L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 3L, 2L, 2L, 3L, 2L, 2L, 
                               3L, 3L, 1L, 1L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                               1L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 
                               2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
                               1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 2L, 
                               3L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("1", "2", "3"), class = "factor"), 
           survival = c(2282L, 2006L, 1456L, 148L, 1863L, 1933L, 416L, 
                        2556L, 2563L, 2372L, 2247L, 1297L, 1877L, 2234L, 2297L, 2014L, 
                        884L, 2450L, 2235L, 901L, 1785L, 1722L, 2372L, 392L, 1959L, 
                        1897L, 1889L, 275L, 2668L, 1732L, 2033L, 436L, 2385L, 2551L, 
                        1955L, 2313L, 1280L, 663L, 875L, 322L, 2234L, 1058L, 1855L, 
                        487L, 1838L, 1826L, 2353L, 2471L, 965L, 2093L, 740L, 820L, 
                        1866L, 1154L, 2138L, 72L, 2556L, 2485L, 464L, 1203L, 1846L, 
                        2449L, 2286L, 1266L, 2169L, 1206L, 2034L, 1990L, 1976L, 2539L, 
                        2467L, 2276L, 2132L, 696L, 1022L, 2217L, 296L, 2320L, 860L, 
                        1341L, 966L, 1388L, 2085L, 535L, 1653L, 1904L, 1868L, 1767L, 
                        1852L, 1481L, 2380L, 1679L, 1298L, 2138L, 2175L, 1083L, 46L, 
                        2144L, 975L, 945L, 1905L, 2659L, 2521L, 2401L, 1499L, 1856L, 
                        1497L, 2148L, 2126L, 1975L, 2438L, 2233L, 359L, 2170L, 829L, 
                        1449L, 1949L, 909L, 2359L, 2551L, 1316L, 1869L, 588L, 1377L, 
                        323L, 1647L, 1680L, 1730L, 1863L, 2388L, 1013L, 1977L, 2087L, 
                        1965L, 773L, 349L, 2017L, 1152L, 1401L, 2128L, 1965L, 2161L, 
                        1905L, 1997L, 2065L, 918L, 760L, 1283L, 1527L, 1074L, 1821L, 
                        1364L, 1441L, 2612L, 1288L, 1637L, 2456L, 2192L, 1717L, 1858L, 
                        2049L, 2388L, 2296L, 1884L, 1328L, 2392L, 2239L, 2237L, 549L, 
                        1514L, 1617L, 1656L, 978L, 1760L, 1013L, 779L, 2127L, 2213L, 
                        2456L, 2205L, 621L, 988L, 2057L, 605L, 2011L, 2010L, 2009L, 
                        1984L, 2030L, 1896L, 1663L, 2129L, 1155L, 1182L, 71L, 114L, 
                        63L, 1655L, 1618L, 638L, 1627L, 855L, 2370L, 853L, 692L, 
                        785L, 1861L, 1842L, 1735L, 1693L, 859L, 1109L, 1211L, 2054L, 
                        1123L, 1814L, 890L, 1114L, 2271L, 17L, 1931L, 1584L, 1016L, 
                        666L, 168L, 1169L, 1675L, 1862L, 629L, 466L, 742L, 936L, 
                        195L, 668L, 1207L, 1427L, 1722L, 1692L, 177L, 697L, 2138L, 
                        602L, 663L, 1598L, 1774L, 2101L, 1370L, 716L, 2227L, 1993L, 
                        1841L, 2177L, 2052L, 1298L, 2030L, 2245L, 1521L, 1980L, 1538L, 
                        2172L, 2161L, 513L, 2014L, 702L, 1810L, 1926L, 1546L, 1852L, 
                        2114L, 2237L, 933L, 1481L, 1807L, 729L, 1441L, 1277L, 1486L, 
                        1781L, 1922L, 2048L, 1194L, 1765L, 2089L, 425L, 2195L, 758L, 
                        648L, 761L, 596L, 462L, 651L, 1897L, 369L, 1812L, 853L, 2059L, 
                        1177L, 1120L, 523L, 1278L, 186L, 1280L, 727L, 1701L, 2015L, 
                        1956L, 2065L, 2153L, 1764L, 140L, 1833L, 2056L, 1729L, 2024L, 
                        2160L, 2027L, 2007L, 973L, 2156L, 1499L, 424L, 2023L, 569L, 
                        1625L, 1938L, 1399L, 1993L, 2192L, 1859L, 1675L, 42L, 436L, 
                        868L, 1560L, 487L, 1629L, 677L, 1820L, 1756L, 375L, 1323L, 
                        1233L, 986L, 650L, 1124L, 360L, 1840L, 273L, 177L, 728L, 
                        1185L, 631L, 1853L, 1854L, 1645L, 683L, 1666L, 549L, 1167L, 
                        1672L, 967L, 1720L, 1596L, 618L, 1502L, 229L, 1838L, 1833L, 
                        813L, 426L, 1834L, 2030L, 1441L, 1666L, 1979L, 1703L, 919L, 
                        1791L, 1685L, 1850L, 652L, 1534L, 622L, 646L, 792L, 888L, 
                        1030L, 806L, 1163L, 1721L, 741L, 677L, 970L, 892L, 753L, 
                        1703L, 1720L, 1355L, 1603L, 1570L, 1350L, 1341L, 1093L, 792L, 
                        1105L, 1434L, 67L, 623L, 1582L, 1771L, 1575L, 1407L, 747L, 
                        1578L, 1278L, 1190L, 1208L, 777L, 722L, 717L, 1959L, 2051L, 
                        740L, 1529L, 1718L, 1940L, 1631L, 1826L, 1231L, 1117L, 1201L, 
                        1222L, 1981L, 929L, 1044L, 1818L, 1493L, 1432L, 801L, 1786L, 
                        1847L, 2009L, 1926L, 1818L, 1100L, 1499L, 1848L, 1789L, 1707L, 
                        1714L, 1717L, 329L, 854L, 877L, 432L, 521L, 751L, 928L, 1722L, 
                        294L, 1706L, 1702L, 1222L, 1089L, 1243L, 995L, 1088L, 1842L, 
                        1821L, 1806L, 1604L, 1743L, 1781L, 906L, 57L, 1152L, 1685L, 
                        1434L, 1604L, 772L, 1302L, 1030L, 1611L, 1735L, 1180L, 1377L, 
                        1250L, 646L, 1502L, 1364L, 1741L, 1729L, 1642L, 877L, 1137L, 
                        311L, 1436L, 1356L, 1352L, 1077L, 867L, 915L, 1645L, 1356L, 
                        1632L, 967L, 1091L, 1065L, 600L, 1219L, 1026L, 1624L, 1715L, 
                        534L, 962L, 1490L, 441L, 1240L, 1751L, 1878L, 1684L, 1701L, 
                        1701L, 1693L, 508L, 1638L, 1474L, 1612L, 1483L, 675L, 1155L, 
                        1472L, 1545L, 1067L, 1338L, 1459L, 1192L, 1264L, 1095L, 1078L, 
                        1624L, 1600L, 1469L, 1475L, 1171L, 1751L, 1756L, 798L, 940L, 
                        766L, 1173L, 1340L, 818L, 1557L, 1169L, 1094L, 1730L, 1483L, 
                        1624L, 601L, 1331L, 604L, 652L, 657L, 567L, 429L, 566L, 15L, 
                        310L, 1296L, 488L, 1505L, 1293L, 1570L, 1469L, 1472L, 1342L, 
                        1349L, 1249L, 1342L, 1402L, 1232L, 996L, 1351L, 507L, 733L, 
                        526L, 463L, 529L, 623L, 546L, 213L, 276L, 942L, 570L, 1177L, 
                        1113L, 558L, 723L, 530L, 1443L, 1317L, 1344L, 1358L, 796L, 
                        965L, 1435L, 541L, 1329L, 1357L, 1306L, 870L, 1148L, 734L, 
                        1338L, 490L, 210L, 368L, 432L, 319L, 65L, 1343L, 748L, 737L, 
                        461L, 649L, 842L, 918L, 1104L, 1089L, 659L, 1212L, 1119L, 
                        974L, 1230L, 1205L, 1090L, 1095L, 701L, 972L, 825L, 889L, 
                        762L, 566L, 855L, 740L, 1305L, 1219L, 1195L, 1088L, 628L, 
                        1125L, 916L, 972L, 553L, 745L, 711L, 828L, 651L, 637L, 615L, 
                        740L, 1062L, 8L, 936L, 867L, 616L, 453L, 758L, 517L, 969L, 
                        974L, 1124L, 504L, 721L, 796L, 841L, 695L, 16L, 29L, 18L, 
                        17L, 857L, 768L, 858L, 770L), status_new = c(1, 1, 2, 1, 
                                                                     1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 
                                                                     2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 
                                                                     2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 2, 2, 1, 
                                                                     1, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1, 2, 2, 
                                                                     2, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 
                                                                     2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2, 
                                                                     1, 2, 2, 1, 2, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1, 
                                                                     1, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 1, 2, 1, 
                                                                     1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 
                                                                     2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 
                                                                     1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 
                                                                     1, 2, 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 
                                                                     1, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 
                                                                     1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2, 1, 
                                                                     1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 
                                                                     2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1, 1, 1, 2, 
                                                                     1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 2, 2, 
                                                                     2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 
                                                                     1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 
                                                                     2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 
                                                                     1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 
                                                                     2, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 
                                                                     2, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 
                                                                     1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 
                                                                     1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 
                                                                     2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 
                                                                     2, 1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 
                                                                     1, 1, 2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 
                                                                     1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 
                                                                     1, 1, 1, 2, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 
                                                                     2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 
                                                                     2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 
                                                                     1, 1, 2, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 
                                                                     1, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 
                                                                     2, 2, 1, 1, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 
                                                                     2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1)), row.names = c(NA, 
                                                                                                                                        -686L), class = "data.frame") 


library(survival)
library(survminer)
b$status_new<-as.numeric(b$status_new)
km2 <- survfit(Surv(survival, status_new) ~ grade, data = b)
ggrisktable(km2, 
        data = b, 
        color = "grade", 
        palette = c("red", "yellow", "green"),
        legend.title = "Grade",
        legend.labs = c("Grade 1", "Grade 2", "Grade 3"))

谢谢

请记住 ggrisktable 只是一个包装轮 ggplot。您在图例中得到的奇怪的“a”是彩色文本的图例键字形。这意味着键中没有彩色点、方块或线来显示哪种颜色与哪个因子水平相关,而是有一个彩色字母“a”。

如果您直接在 ggplot 中绘制此图,这将很容易更改,但由于您是在 ggrisktable 中绘制的,因此有点棘手。实际上,您需要 re-specify geom_text 层,

在您的情况下,我们可以通过以下操作将“a”符号替换为 filled-in 方块:

p <- ggrisktable(km2, 
        data = b, 
        palette = c("indianred", "gold", "forestgreen"),
        legend.title = "Grade",
        legend.labs = c("Grade 1", "Grade 2", "Grade 3")) 

p$layers[[1]] <- geom_text(aes(colour = strata), key_glyph = "rect")

p

要完全删除图例,您可以执行以下操作:

p + theme(legend.position = "none")