ggplot中文本标签不同部分的不同字体大小
Posted
技术标签:
【中文标题】ggplot中文本标签不同部分的不同字体大小【英文标题】:Different font sizes for different portions of text label in ggplot 【发布时间】:2020-03-27 22:52:58 【问题描述】:已提供Data sample
,请滚动至底部
我已经关注了这些instructions,但我无法让它与我的脚本一起运行。他们似乎正在使用 labs() 而不是我在下面使用的方法。
我已经制作了这个情节:
但是,我希望文本的不同部分使用不同的字体大小。我在 Photoshop 中编辑了以下内容,描绘了我正在寻找的结果。
我使用了以下脚本:
ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
geom_line(aes(y = yhat, col = model),size=1) +
ggtitle("Lymph node yield") +
geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +
geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +
scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +
scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +
scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25\nas reference","50", "75", "100")) +
scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +
theme(axis.text.x = element_text(color = "grey20", size =11),
axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
axis.text.y = element_text(color = "grey20", size = 11),
axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
legend.key = element_rect(fill = "white"),
plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top") +
guides(fill=guide_legend(nrow=2,byrow=TRUE))
数据样本
out <- structure(list(model = c("1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4"), n.fjernet = c(0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46
), yhat = c(1.06995503357619, 1.06706507368365, 1.06418291960344,
1.06130855025195, 1.05844194460257, 1.05558308168545, 1.05273194058739,
1.04988850045166, 1.0470527404779, 1.0442246399219, 1.0414041780955,
1.0385913343664, 1.03578608815804, 1.03298841894944, 1.03019830627504,
1.02741572972456, 1.02464066894286, 1.02187310362975, 1.01911301353991,
1.01636037848267, 1.01361517832191, 1.0108773929759, 1.00814700241714,
1.00542398667224, 1.00270832582174, 1, 0.997298989395025, 0.994605274248338,
0.991918834854829, 0.989239651562611, 0.986567704772878, 0.983902974939761,
0.981245442570182, 0.978595088223716, 0.975951892512447, 0.973315836100825,
0.970686899705527, 0.968065064095311, 0.965450310090884, 0.962842618564752,
0.960241970441086, 0.957648346695583, 0.955061728355321, 0.952482096498627,
0.949909432254935, 0.947343716804649, 0.944784931379003, 0.942233057259927,
0.93968807577991, 0.937149968321859, 0.93461871631897, 0.932094301254584,
0.929576704662058, 0.927065908124628, 0.924561893275272, 0.92206464179658,
0.919574135420615, 0.917090355928783, 0.914613285151699, 0.912142904969053,
0.909679197309478, 0.90722214415042, 0.904771727518001, 0.902327929486893,
0.899890732180184, 0.897460117769246, 0.895036068473609, 0.892618566560827,
0.890207594346348, 0.887803134193389, 0.885405168512802, 0.88301367976295,
0.880628650449572, 0.878250063125662, 0.87587790039134, 0.873512144893719,
0.871152779326786, 0.868799786431271, 0.86645314899452, 0.864112849850372,
0.86177887187903, 0.859451198006942, 0.857129811206666, 0.854814694496757,
0.852505830941632, 0.850203203651456, 0.847906795782009, 0.845616590534571,
0.843332571155794, 0.841054720937581, 0.838783023216965, 0.836517461375982,
0.834258018841559, 0.832004679085382, 0.829757425623784, 0.827516242017617,
0.825281111872138, 0.823052018836886, 0.820828946605561, 0.818611878915908,
0.816400799549598, 1.09390435986962, 1.08998414105606, 1.08607797110829,
1.08218579967948, 1.07830757660322, 1.07444325189287, 1.07059277574093,
1.06675609851842, 1.06293317077417, 1.05912394323428, 1.05532836680139,
1.0515463925541, 1.04777797174635, 1.04402305580675, 1.04028159633797,
1.03655354511615, 1.03283885409023, 1.02913747538134, 1.02544936128221,
1.02177446425654, 1.01811273693838, 1.01446413213151, 1.01082860280888,
1.00720610211193, 1.00359658335007, 1, 0.996416305705164, 0.992845454275126,
0.989287399684986, 0.985742096074781, 0.982209497748898, 0.978689559175481,
0.975182234985848, 0.971687479973904, 0.968205249095557, 0.964735497468143,
0.96127818036984, 0.957833253239098, 0.95440067167406, 0.950980391431994,
0.947572368428718, 0.944176558738035, 0.940792918591168, 0.93742140437619,
0.93406197263747, 0.930714580075105, 0.927379183544369, 0.924055740055151,
0.920744206771404, 0.917444541010594, 0.914156700243146, 0.910880642091898,
0.907616324331556, 0.904363704888149, 0.901122741838484, 0.89789339340961,
0.894675617978277, 0.891469374070399, 0.888274620360521, 0.885091315671288,
0.881919418972907, 0.878758889382628, 0.875609686164211, 0.872471768727401,
0.869345096627407, 0.866229629564379, 0.863125327382891, 0.86003215007142,
0.856950057761833, 0.853879010728873, 0.850818969389643, 0.847769894303103,
0.844731746169555, 0.84170448583014, 0.838688074266332, 0.835682472599437,
0.832687642090087, 0.829703544137748, 0.826730140280216, 0.823767392193125,
0.82081526168945, 0.817873710719019, 0.814942701368018, 0.812022195858507,
0.809112156547929, 0.806212545928625, 0.803323326627355, 0.800444461404812,
0.797575913155142, 0.794717644905469, 0.791869619815415, 0.789031801176629,
0.786204152412307, 0.783386637076731, 0.780579218854788, 0.77778186156151,
0.774994529141605, 0.772217185668991, 0.769449795346334, 0.766692322504588,
0.763944731602534, 1.11341569090863, 1.10864127602262, 1.10388733420672,
1.09915377767079, 1.09444051900111, 1.08974747115884, 1.08507454747834,
1.0804216616656, 1.07578872779666, 1.07117566031599, 1.06658237403496,
1.06200878413019, 1.05745480614207, 1.05292035597315, 1.04840534988657,
1.04390970450458, 1.03943333680694, 1.03497616412939, 1.03053810416218,
1.02611907494849, 1.02171899488293, 1.01733778271005, 1.01297535752282,
1.00863163876117, 1.00430654621045, 1, 0.995711920601629, 0.991442228828184,
0.987190845832071, 0.982957693103798, 0.978742692470529, 0.97454576609464,
0.97036683647228, 0.96620582643194, 0.962062659133031, 0.957937258064461,
0.953829547043222, 0.949739450212989, 0.94566689204271, 0.94161179732522,
0.937574091175846, 0.933553699031029, 0.929550546646941, 0.925564560098119,
0.9215956657761, 0.917643790388057, 0.913708860955451, 0.909790804812679,
0.905889549605734, 0.90200502329087, 0.898137154133269, 0.894285870705718,
0.890451101887291, 0.886632776862031, 0.882830825117648, 0.879045176444214,
0.875275760932866, 0.871522508974516, 0.867785351258566, 0.864064218771626,
0.860359042796242, 0.856669754909625, 0.852996286982389, 0.849338571177293,
0.845696539947986, 0.842070126037761, 0.838459262478315, 0.834863882588508,
0.831283919973136, 0.827719308521702, 0.824169982407196, 0.82063587608488,
0.817116924291076, 0.813613062041963, 0.810124224632375, 0.806650347634607,
0.803191366897227, 0.799747218543885, 0.796317838972142, 0.792903164852291,
0.789503133126184, 0.786117681006076, 0.782746745973459, 0.779390265777908,
0.776048178435935, 0.77272042222984, 0.769406935706576, 0.766107657676608,
0.762822527212791, 0.759551483649236, 0.756294466580198, 0.753051415858953,
0.749822271596694, 0.74660697416142, 0.743405464176839, 0.740217682521265,
0.737043570326536, 0.733883068976917, 0.730736120108023, 0.727602665605742,
0.724482647605159, 1.1320553450125, 1.1264526815186, 1.12087774620809,
1.11533040185123, 1.10981051189742, 1.10431794047186, 1.09885255237224,
1.09341421306535, 1.0880027886838, 1.08261814602275, 1.07726015253658,
1.07192867633566, 1.0666235861831, 1.06134475149152, 1.05609204231981,
1.05086532936997, 1.04566448398391, 1.04048937814028, 1.03533988445131,
1.03021587615971, 1.0251172271355, 1.02004381187294, 1.01499550548744,
1.00997218371247, 1.00497372289651, 1, 0.99505089259232, 0.990126278848773,
0.985226037547583, 0.980350048066917, 0.97549819038191, 0.970670345061712,
0.965866393266552, 0.961086216744807, 0.956329697830096, 0.951596719438381,
0.946887165065084, 0.942200918782223, 0.937537865235555, 0.932897889641737,
0.928280877785502, 0.923686716016846, 0.919115291248231, 0.914566490951803,
0.910040203156617, 0.905536316445888, 0.901054719954242), lower = c(0.931198841176898,
0.933857755314683, 0.936524261627286, 0.939198381793148, 0.941880137552615,
0.944569550708104, 0.947266643124289, 0.949971436728276, 0.952683953509779,
0.955404215521302, 0.958132244878317, 0.960868063759443, 0.963611694406628,
0.966363159125328, 0.96912248028469, 0.971889680317732, 0.974664781721529,
0.977447807057391, 0.980238778951049, 0.983037720092839, 0.985844653237888,
0.988659601206295, 0.99148258688332, 0.994313633219567, 0.997152763231174,
1, 0.99177339754103, 0.983614472070078, 0.975522666835468, 0.967497429665698,
0.959538212931763, 0.951644473509783, 0.943815672743943, 0.936051276409733,
0.928350754677499, 0.920713582076282, 0.913139237457967, 0.905627203961713,
0.898176968978691, 0.890788024117101, 0.883459865167478, 0.87619199206829,
0.868983908871812, 0.861835123710281, 0.85474514876234, 0.847713500219739,
0.840739698254329, 0.833823266985317, 0.826963734446789, 0.82016063255551,
0.813413497078978, 0.806721867603749, 0.800085287504015, 0.793503303910449,
0.786975467679298, 0.780501333361739, 0.774080459173476, 0.767712406964599,
0.761396742189682, 0.755133033878133, 0.748920854604782, 0.742759780460716,
0.736649391024354, 0.730589269332754, 0.724579001853164, 0.718618178454801,
0.712706392380864, 0.70684324022078, 0.701028321882674, 0.695261240566066,
0.689541602734799, 0.683869018090179, 0.678243099544345, 0.672663463193854,
0.667129728293484, 0.661641517230253, 0.65619845549765, 0.650800171670081,
0.645446297377522, 0.640136467280383, 0.634870319044577, 0.629647493316798,
0.624467633699994, 0.619330386729051, 0.614235401846671, 0.609182331379452,
0.604170830514165, 0.599200557274219, 0.594271172496331, 0.589382339807378,
0.584533725601445, 0.579724999017061, 0.574955831914621, 0.570225898853993,
0.565534877072313, 0.560882446461956, 0.556268289548699, 0.551692091470051,
0.547153539953769, 0.542652325296551, 0.538188140342901, 0.961892022918095,
0.963388083737925, 0.964886471428051, 0.966387189607529, 0.967890241901041,
0.969395631938907, 0.970903363357095, 0.972413439797228, 0.97392586490659,
0.975440642338142, 0.976957775750523, 0.978477268808065, 0.979999125180797,
0.981523348544458, 0.983049942580502, 0.984578910976112, 0.986110257424203,
0.987643985623434, 0.989180099278218, 0.990718602098728, 0.992259497800909,
0.993802790106485, 0.995348482742968, 0.996896579443668, 0.998447083947701,
1, 0.99130364863173, 0.982682923790581, 0.974137167801699, 0.965665728709605,
0.957267960228449, 0.948943221692716, 0.940690878008338, 0.932510299604252,
0.924400862384363, 0.916361947679937, 0.9083929422024, 0.900493237996551,
0.892662232394182, 0.884899327968098, 0.877203932486542, 0.869575458868011,
0.86201332513647, 0.854516954376953, 0.847085774691547, 0.839719219155767,
0.832416725775299, 0.825177737443132, 0.818001701897053, 0.810888071677514,
0.803836304085867, 0.796845861142965, 0.789916209548114, 0.783046820638392,
0.776237170348314, 0.769486739169854, 0.762795012112809, 0.756161478665512,
0.749585632755886, 0.743066972712834, 0.736605001227967, 0.730199225317664,
0.723849156285463, 0.717554309684779, 0.711314205281944, 0.705128367019571,
0.698996322980234, 0.692917605350469, 0.686891750385082, 0.680918298371767,
0.674996793596042, 0.669126784306475, 0.663307822680226, 0.657539464788876,
0.651821270564568, 0.646152803766427, 0.640533631947281, 0.634963326420674,
0.629441462228154, 0.623967618106861, 0.618541376457381, 0.613162323311894,
0.60783004830259, 0.602544144630358, 0.597304209033759, 0.592109841758255,
0.586960646525714, 0.58185623050418, 0.576796204277899, 0.571780181817614,
0.566807780451114, 0.561878620834042, 0.556992326920951, 0.552148525936616,
0.547346848347599, 0.542586927834053, 0.537868401261778, 0.533190908654516,
0.52855409316649, 0.523957601055177, 0.519401081654325, 0.963917275108263,
0.965335267842061, 0.966755346546767, 0.968177514290995, 0.969601774147877,
0.971028129195062, 0.972456582514729, 0.973887137193588, 0.975319796322895,
0.976754562998448, 0.978191440320602, 0.979630431394273, 0.981071539328944,
0.982514767238671, 0.983960118242094, 0.985407595462437, 0.986857202027521,
0.988308941069769, 0.989762815726209, 0.991218829138486, 0.992676984452867,
0.994137284820245, 0.99559973339615, 0.997064333340755, 0.99853108781888,
1, 0.989985887261381, 0.980072056976705, 0.970257504906171, 0.96054123686655,
0.950922268630476, 0.941399625826748, 0.931972343841626, 0.922639467721122,
0.913400052074264, 0.904253160977332, 0.895197867879053, 0.886233255506742,
0.877358415773384, 0.868572449685654, 0.859874467252844, 0.851263587396715,
0.842738937862244, 0.834299655129267, 0.825944884325013, 0.817673779137497,
0.809485501729802, 0.801379222655203, 0.793354120773147, 0.785409383166078,
0.777544205057084, 0.769757789728383, 0.762049348440613, 0.754418100352938,
0.74686327244395, 0.739384099433363, 0.731979823704495, 0.724649695227524,
0.717392971483511, 0.710208917389182, 0.703096805222475, 0.696055914548815,
0.689085532148141, 0.682184951942659, 0.675353474925316, 0.668590409088996,
0.66189506935642, 0.655266777510749, 0.648704862126886, 0.642208658503457,
0.635777508595487, 0.629410760947734, 0.623107770628704, 0.616867899165319,
0.610690514478243, 0.604574990817853, 0.598520708700853, 0.592527054847525,
0.586593422119601, 0.580719209458763, 0.574903821825762, 0.569146670140136,
0.563447171220544, 0.557804747725686, 0.552218828095824, 0.546688846494885,
0.54121424275314, 0.535794462310464, 0.53042895616016, 0.525117180793344,
0.519858598143894, 0.514652675533941, 0.509498885619913, 0.504396706339115,
0.499345620856847, 0.494345117514051, 0.48939468977548, 0.484493836178387,
0.479642060281731, 0.474838870615887, 0.470083780632861, 0.991175581844016,
0.991527058864988, 0.991878660521896, 0.992230386858937, 0.992582237920323,
0.992934213750281, 0.993286314393057, 0.993638539892908, 0.99399089029411,
0.994343365640955, 0.994695965977748, 0.995048691348811, 0.995401541798484,
0.995754517371118, 0.996107618111084, 0.996460844062767, 0.996814195270567,
0.997167671778901, 0.997521273632202, 0.997875000874918, 0.998228853551512,
0.998582831706465, 0.99893693538427, 0.999291164629441, 0.999645519486503,
1, 0.98977529837702, 0.979655141277318, 0.969638459764339, 0.959724195831082,
0.949911302288355, 0.94019874265416, 0.93058549104422, 0.921070532063618,
0.911652860699548, 0.902331482215158, 0.893105412044486, 0.883973675688463,
0.874935308611979, 0.865989356142011, 0.857134873366782, 0.848370925035956,
0.839696585461851, 0.831110938421669, 0.822613077060712, 0.814202103796604,
0.805877130224481), upper = c(1.22938702589896, 1.21927334756868,
1.20924287044941, 1.19929491007788, 1.1894287876216, 1.17964382983259,
1.16993936900138, 1.1603147429115, 1.15076929479429, 1.14130237328403,
1.13191333237354, 1.1226015313701, 1.11336633485168, 1.10420711262366,
1.09512323967574, 1.08611409613934, 1.07717906724532, 1.06831754328197,
1.05952891955345, 1.0508125963385, 1.04216797884954, 1.03359447719208,
1.02509150632444, 1.01665848601784, 1.00829484081683, 1, 1.00285536667381,
1.00571888646646, 1.00859058265809, 1.01147047859533, 1.01435859769145,
1.01725496342659, 1.02015959934792, 1.02307252906987, 1.02599377627426,
1.02892336471057, 1.03186131819607, 1.03480766061603, 1.03776241592396,
1.04072560814172, 1.04369726135978, 1.04667739973741, 1.04966604750285,
1.05266322895352, 1.05566896845622, 1.05868329044732, 1.06170621943298,
1.06473777998932, 1.06777799676265, 1.07082689446963, 1.07388449789751,
1.07695083190433, 1.08002592141908, 1.08310979144195, 1.0862024670445,
1.08930397336991, 1.09241433563312, 1.09553357912107, 1.09866172919293,
1.10179881128026, 1.10494485088723, 1.10809987359085, 1.11126390504115,
1.11443697096141, 1.11761909714835, 1.12081030947236, 1.12401063387769,
1.12722009638267, 1.13043872307992, 1.13366654013659, 1.13690357379451,
1.14014985037045, 1.14340539625635, 1.14667023791947, 1.14994440190267,
1.15322791482459, 1.15652080337989, 1.15982309433943, 1.16313481455051,
1.16645599093713, 1.16978665050011, 1.17312682031742, 1.1764765275443,
1.17983579941357, 1.18320466323578, 1.18658314639948, 1.18997127637141,
1.19336908069675, 1.19677658699932, 1.20019382298183, 1.20362081642608,
1.20705759519321, 1.21050418722389, 1.21396062053859, 1.21742692323779,
1.2209031235022, 1.224389249593, 1.22788532985205, 1.23139139270217,
1.23490746664731, 1.23843358027281, 1.24403438227042, 1.23321582216799,
1.22249134406551, 1.21186012979284, 1.20132136829497, 1.19087425557006,
1.1805179946082, 1.17025179533052, 1.16007487452898, 1.14998645580657,
1.13998576951813, 1.13007205271157, 1.12024454906973, 1.11050250885263,
1.1008451888403, 1.09127185227608, 1.08178176881038, 1.07237421444502,
1.06304847147793, 1.05380382844846, 1.04463958008304, 1.03555502724144,
1.02654947686337, 1.01762224191565, 1.00877264133979, 1, 1.00155533135132,
1.00311308175825, 1.00467325498323, 1.00623585479454, 1.00780088496633,
1.0093683492786, 1.01093825151727, 1.0125105954741, 1.01408538494679,
1.01566262373891, 1.01724231565998, 1.01882446452542, 1.02040907415658,
1.02199614838079, 1.0235856910313, 1.02517770594732, 1.02677219697406,
1.02836916796268, 1.02996862277034, 1.03157056526021, 1.03317499930146,
1.03478192876928, 1.03639135754487, 1.0380032895155, 1.03961772857446,
1.0412346786211, 1.04285414356084, 1.04447612730518, 1.04610063377168,
1.04772766688402, 1.04935723057197, 1.05098932877142, 1.05262396542436,
1.05426114447894, 1.05590086988942, 1.05754314561625, 1.059187975626,
1.06083536389143, 1.06248531439148, 1.06413783111128, 1.06579291804213,
1.06745057918158, 1.06911081853336, 1.07077364010747, 1.07243904792009,
1.0741070459937, 1.07577763835701, 1.077450829045, 1.07912662209892,
1.08080502156631, 1.08248603150102, 1.08416965596318, 1.08585589901925,
1.08754476474202, 1.08923625721058, 1.09093038051042, 1.09262713873334,
1.09432653597751, 1.0960285763475, 1.09773326395424, 1.09944060291505,
1.10115059735368, 1.10286325140027, 1.10457856919139, 1.10629655487005,
1.1080172125857, 1.10974054649424, 1.11146656075804, 1.11319525954593,
1.11492664703324, 1.11666072740179, 1.11839750483991, 1.12013698354243,
1.12187916771071, 1.12362406155264, 1.28610051170864, 1.27322135619119,
1.26047117398908, 1.24784867354897, 1.23535257625132, 1.22298161628079,
1.2107345404981, 1.19861010831301, 1.18660709155872, 1.1747242743674,
1.1629604530471, 1.15131443595973, 1.13978504340043, 1.12837110747803,
1.11707147199674, 1.10588499233907, 1.09481053534984, 1.08384697922142,
1.07299321338009, 1.06224813837353, 1.05161066575947, 1.04107971799542,
1.03065422832952, 1.0203331406925, 1.01011540959066, 1, 1.00147107305825,
1.00294431017245, 1.00441971452608, 1.0058972893073, 1.00737703770897,
1.00885896292865, 1.01034306816859, 1.01182935663577, 1.01331783154186,
1.0148084961033, 1.0163013535412, 1.01779640708146, 1.01929365995471,
1.02079311539632, 1.02229477664643, 1.02379864694995, 1.02530472955655,
1.02681302772071, 1.02832354470165, 1.02983628376343, 1.03135124817489,
1.03286844120968, 1.03438786614626, 1.03590952626794, 1.03743342486282,
1.03895956522387, 1.04048795064888, 1.04201858444052, 1.04355146990629,
1.04508661035857, 1.04662400911462, 1.04816366949655, 1.04970559483138,
1.05124978845104, 1.05279625369233, 1.05434499389697, 1.05589601241159,
1.05744931258777, 1.05900489778199, 1.06056277135567, 1.0621229366752,
1.0636853971119, 1.06525015604205, 1.06681721684691, 1.06838658291269,
1.06995825763062, 1.07153224439687, 1.07310854661266, 1.07468716768416,
1.0762681110226, 1.07785138004418, 1.07943697817017, 1.08102490882684,
1.08261517544551, 1.08420778146257, 1.08580273031943, 1.08740002546258,
1.08899967034358, 1.09060166841908, 1.09220602315077, 1.09381273800549,
1.09542181645515, 1.09703326197676, 1.09864707805247, 1.10026326816952,
1.10188183582031, 1.10350278450237, 1.10512611771836, 1.10675183897611,
1.1083799517886, 1.11001045967398, 1.11164336615559, 1.11327867476193,
1.1149163890267, 1.11655651248881, 1.29295891428957, 1.27973879518019,
1.26665384784412, 1.25370269019031, 1.24088395425919, 1.22819628607814,
1.21563834551854, 1.20320880615416, 1.19090635512109, 1.17872969297907,
1.16667753357421, 1.15474860390318, 1.14294164397872, 1.13125540669655,
1.1196886577037, 1.10824017526804, 1.09690875014933, 1.08569318547141,
1.07459229659587, 1.06360491099682, 1.05272986813714, 1.04196601934584,
1.03131222769674, 1.02076736788841, 1.01033032612528, 1, 1.00035460621449,
1.00070933817455, 1.00106419592476, 1.00141917950974, 1.0017742889741,
1.00212952436249, 1.00248488571955, 1.00284037308996, 1.0031959865184,
1.00355172604957, 1.00390759172819, 1.00426358359899, 1.00461970170672,
1.00497594609614, 1.00533231681204, 1.00568881389921, 1.00604543740246,
1.00640218736662, 1.00675906383654, 1.00711606685707, 1.00747319647309
)), row.names = c(NA, 350L), class = c("Predict", "data.frame"))
【问题讨论】:
我认为方法还是一样的。使用ggplot_build()
和ggplot_gtable()
创建一个所谓的gtable
,然后查看存储文本的gtable
对象,并像在链接中那样进行调整。
感谢您输入@ricoderks。你有机会举例说明如何做吗?
我觉得我有点太快了。我现在正在玩它,但是在查找文本的位置时遇到问题。还尝试从您的链接中复制代码,但它不再起作用了。
对不起,我没有成功。我也玩过expression
,但这里的线条错位了。
你有示例数据吗?我很想玩这个,但我很感激不必从头开始构建一个 df 来匹配你的情节。
【参考方案1】:
您可以使用当前正在开发的 ggtext 包来执行此操作。
# this requires the current development versions of ggplot2 and ggtext
# remotes::install_github("tidyverse/ggplot2")
# remotes::install_github("clauswilke/ggtext")
library(tidyverse)
library(ggtext)
df <- tibble(reference = seq(0, 100, by = 5)) %>%
mutate(
overall_not_adj = 1 + .08/(-25) * (reference - 25),
event_free_not_adj = 1 + .1/(-25) * (reference - 25),
overall_adj = 1 + .12/(-25) * (reference - 25),
event_free_adj = 1 + .14/(-25) * (reference - 25)
) %>%
pivot_longer(-reference, names_to = "lymph_node_yield", values_to = "hazard_ratio")
ggplot(df, aes(reference, hazard_ratio, color = lymph_node_yield)) +
geom_line() +
scale_color_manual(
name = "Lymph node yield",
breaks = c(
"overall_not_adj", "overall_adj",
"event_free_not_adj", "event_free_adj"
),
labels = c(
"Overall survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
"Overall survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>",
"Event-free survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
"Event-free survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>"
),
values = c(
overall_not_adj = "#0072B2", overall_adj = "#009E73",
event_free_not_adj = "#CC79A7", event_free_adj = "#E69F00"
),
guide = guide_legend(title.position = "top", ncol = 2)
) +
theme_classic() +
theme(
legend.position = "top",
legend.title.align = 0.5,
legend.text = element_markdown(),
legend.key.height = grid::unit(20, "pt")
)
由reprex package (v0.3.0) 于 2019-12-03 创建
【讨论】:
以上是关于ggplot中文本标签不同部分的不同字体大小的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ggplot facet wrap 标签中使用不同的字体大小?
R语言ggplot2可视化柱状图添加数值说明并控制文本数值字体大小轴标签字体大小实战