如何在 ggplot 标签文本的表达式(粘贴(斜体()))中添加换行符?
How to add a newline escape in expression(paste(italic())) for ggplot label text?
我有这个情节:
如您所见,我已将 c(expression(paste(italic("TERT"),"p-wt", "\nn = 504")), expression(paste(italic("TERT"),"-alt"))))
应用于 italicize TERT 标签文本。我需要像 \n
一样对标签文本应用换行转义。子序列标签文本 n = 504
不应为斜体,因此它看起来像这样:
您可以在 paste
中看到我的尝试 ... "\nn = 504"
。
如何解决这个问题?
脚本
library(tidyverse)
ggplot(p, aes(time, P)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = CIF), alpha = 0.175) +
geom_line(aes(color = CIF), size=.9) +
scale_y_continuous(name="Absolute risk of recurrence-free death", breaks = seq(0,.30,.05),
labels=c("0%", "5%", "10%", "15%", "20%", "25%", "30%")) +
scale_x_continuous(name="Time in months", breaks = seq(0,84,6)) +
coord_cartesian(xlim=c(0,84), ylim=c(0,.3)) +
scale_color_manual(name = "",
values=c("#2C77BF", "#E38072"),
labels=c(expression(paste(italic("TERT"),"p-wt", "\nn = 504")), expression(paste(italic("TERT"),"-alt")))) +
scale_fill_manual(name = "",
values = c("#2C77BF", "#E38072"),
labels=c(expression(paste(italic("TERT"),"p-wt","\nn = 504")), expression(paste(italic("TERT"),"-alt"))))
数据
p <- structure(list(CIF = c("CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0"), P = c(0.107692307692308,
0.0318799398246248, 0.104250176996486, 0.0318799398246248, 0.0638830432377014,
0.0238177738177738, 0.0923076923076923, 0.0318799398246248, 0.0360585922036334,
0.0996677826035525, 0.0996677826035525, 0.0615384615384615, 0.0382025715809015,
0.0278199117586866, 0.0954782533502835, 0.143589743589744, 0.00595238095238095,
0.0178571428571429, 0.0424905303354376, 0.0615384615384615, 0.0218253968253968,
0.066596150754335, 0.044647744153306, 0.107692307692308, 0.0468049579711745
), time = c(21.37, 15, 131, 11.37, 51, 2.4, 20, 13, 16.89, 103,
103, 3, 21, 6, 94, 82.8, 0.5, 1.233333333, 23, 1.56, 2.02, 58,
24, 27.6, 25.96666667), var = c(0.00147837960855712, 6.15061126865662e-05,
0.000249645579418861, 6.15061126865662e-05, 0.000128780021887127,
4.61483225334451e-05, 0.00128903049613109, 6.15061126865662e-05,
6.96689640374372e-05, 0.000231580946236407, 0.000231580946236407,
0.000888484296768321, 7.39453689578964e-05, 5.37466343441789e-05,
0.000216441563404178, 0.0019532358940643, 1.17399803836879e-05,
3.47981454486557e-05, 8.24380449006048e-05, 0.000888484296768321,
4.23592239658944e-05, 0.00013534809548244, 8.67087414414869e-05,
0.00147837960855712, 9.09589688896577e-05), lower = c(0.0528514417396508,
0.0196518457186032, 0.0772750945993973, 0.0196518457186032, 0.045016193151503,
0.0135965306863706, 0.0425647784155885, 0.0196518457186032, 0.0228690129108758,
0.0737220048215361, 0.0737220048215361, 0.0235520073396644, 0.0245335443794,
0.016570605417229, 0.070434217792914, 0.0775445493926726, 0.00192365089076652,
0.00933140136537858, 0.027903654874056, 0.0235520073396644, 0.0121462452612258,
0.047198554448097, 0.0296158713071857, 0.0528514417396508, 0.0313396074133714
), upper = c(0.212668574724397, 0.0515142923656493, 0.139902848146699,
0.0515142923656493, 0.0902751063867387, 0.041559299113097, 0.193977353098234,
0.0515142923656493, 0.0566316095215483, 0.134062034000531, 0.134062034000531,
0.155705170149612, 0.0592526431191566, 0.0465232824968026, 0.128790443953785,
0.257453579417542, 0.0183406721597764, 0.0340375487466844, 0.0644459736704804,
0.155705170149612, 0.0390636938444939, 0.093565227984076, 0.0670412417437027,
0.212668574724397, 0.0696230518381585), n.risk = c(23, 378, 36,
412, 194, 478, 24, 395, 363, 74, 74, 56, 336, 453, 100, 3, 499,
491, 330, 58, 481, 177, 324, 17, 309), n.event = c(1, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
)), row.names = c(NA, -25L), class = c("data.table", "data.frame"
))
使用atop
将文本置于其他文本之上。参见 ?plotmath
。
ggplot(p, aes(time, P)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = CIF), alpha = 0.175) +
geom_line(aes(color = CIF), size=.9) +
scale_y_continuous(name="Absolute risk of recurrence-free death", breaks = seq(0,.30,.05),
labels=c("0%", "5%", "10%", "15%", "20%", "25%", "30%")) +
scale_x_continuous(name="Time in months", breaks = seq(0,84,6)) +
coord_cartesian(xlim=c(0,84), ylim=c(0,.3)) +
scale_color_manual(name = "",
values=c("#2C77BF", "#E38072"),
labels=c(expression(atop(paste(italic("TERT"),"p-wt"), "n = 504")), expression(paste(italic("TERT"),"-alt")))) +
scale_fill_manual(name = "",
values = c("#2C77BF", "#E38072"),
labels=c(expression(atop(paste(italic("TERT"),"p-wt"),"\nn = 504")), expression(paste(italic("TERT"),"-alt"))))
我有这个情节:
如您所见,我已将 c(expression(paste(italic("TERT"),"p-wt", "\nn = 504")), expression(paste(italic("TERT"),"-alt"))))
应用于 italicize TERT 标签文本。我需要像 \n
一样对标签文本应用换行转义。子序列标签文本 n = 504
不应为斜体,因此它看起来像这样:
您可以在 paste
中看到我的尝试 ... "\nn = 504"
。
如何解决这个问题?
脚本
library(tidyverse)
ggplot(p, aes(time, P)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = CIF), alpha = 0.175) +
geom_line(aes(color = CIF), size=.9) +
scale_y_continuous(name="Absolute risk of recurrence-free death", breaks = seq(0,.30,.05),
labels=c("0%", "5%", "10%", "15%", "20%", "25%", "30%")) +
scale_x_continuous(name="Time in months", breaks = seq(0,84,6)) +
coord_cartesian(xlim=c(0,84), ylim=c(0,.3)) +
scale_color_manual(name = "",
values=c("#2C77BF", "#E38072"),
labels=c(expression(paste(italic("TERT"),"p-wt", "\nn = 504")), expression(paste(italic("TERT"),"-alt")))) +
scale_fill_manual(name = "",
values = c("#2C77BF", "#E38072"),
labels=c(expression(paste(italic("TERT"),"p-wt","\nn = 504")), expression(paste(italic("TERT"),"-alt"))))
数据
p <- structure(list(CIF = c("CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=1",
"CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0", "CIF0 2; tert.mut=0",
"CIF0 2; tert.mut=1", "CIF0 2; tert.mut=0"), P = c(0.107692307692308,
0.0318799398246248, 0.104250176996486, 0.0318799398246248, 0.0638830432377014,
0.0238177738177738, 0.0923076923076923, 0.0318799398246248, 0.0360585922036334,
0.0996677826035525, 0.0996677826035525, 0.0615384615384615, 0.0382025715809015,
0.0278199117586866, 0.0954782533502835, 0.143589743589744, 0.00595238095238095,
0.0178571428571429, 0.0424905303354376, 0.0615384615384615, 0.0218253968253968,
0.066596150754335, 0.044647744153306, 0.107692307692308, 0.0468049579711745
), time = c(21.37, 15, 131, 11.37, 51, 2.4, 20, 13, 16.89, 103,
103, 3, 21, 6, 94, 82.8, 0.5, 1.233333333, 23, 1.56, 2.02, 58,
24, 27.6, 25.96666667), var = c(0.00147837960855712, 6.15061126865662e-05,
0.000249645579418861, 6.15061126865662e-05, 0.000128780021887127,
4.61483225334451e-05, 0.00128903049613109, 6.15061126865662e-05,
6.96689640374372e-05, 0.000231580946236407, 0.000231580946236407,
0.000888484296768321, 7.39453689578964e-05, 5.37466343441789e-05,
0.000216441563404178, 0.0019532358940643, 1.17399803836879e-05,
3.47981454486557e-05, 8.24380449006048e-05, 0.000888484296768321,
4.23592239658944e-05, 0.00013534809548244, 8.67087414414869e-05,
0.00147837960855712, 9.09589688896577e-05), lower = c(0.0528514417396508,
0.0196518457186032, 0.0772750945993973, 0.0196518457186032, 0.045016193151503,
0.0135965306863706, 0.0425647784155885, 0.0196518457186032, 0.0228690129108758,
0.0737220048215361, 0.0737220048215361, 0.0235520073396644, 0.0245335443794,
0.016570605417229, 0.070434217792914, 0.0775445493926726, 0.00192365089076652,
0.00933140136537858, 0.027903654874056, 0.0235520073396644, 0.0121462452612258,
0.047198554448097, 0.0296158713071857, 0.0528514417396508, 0.0313396074133714
), upper = c(0.212668574724397, 0.0515142923656493, 0.139902848146699,
0.0515142923656493, 0.0902751063867387, 0.041559299113097, 0.193977353098234,
0.0515142923656493, 0.0566316095215483, 0.134062034000531, 0.134062034000531,
0.155705170149612, 0.0592526431191566, 0.0465232824968026, 0.128790443953785,
0.257453579417542, 0.0183406721597764, 0.0340375487466844, 0.0644459736704804,
0.155705170149612, 0.0390636938444939, 0.093565227984076, 0.0670412417437027,
0.212668574724397, 0.0696230518381585), n.risk = c(23, 378, 36,
412, 194, 478, 24, 395, 363, 74, 74, 56, 336, 453, 100, 3, 499,
491, 330, 58, 481, 177, 324, 17, 309), n.event = c(1, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0
)), row.names = c(NA, -25L), class = c("data.table", "data.frame"
))
使用atop
将文本置于其他文本之上。参见 ?plotmath
。
ggplot(p, aes(time, P)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = CIF), alpha = 0.175) +
geom_line(aes(color = CIF), size=.9) +
scale_y_continuous(name="Absolute risk of recurrence-free death", breaks = seq(0,.30,.05),
labels=c("0%", "5%", "10%", "15%", "20%", "25%", "30%")) +
scale_x_continuous(name="Time in months", breaks = seq(0,84,6)) +
coord_cartesian(xlim=c(0,84), ylim=c(0,.3)) +
scale_color_manual(name = "",
values=c("#2C77BF", "#E38072"),
labels=c(expression(atop(paste(italic("TERT"),"p-wt"), "n = 504")), expression(paste(italic("TERT"),"-alt")))) +
scale_fill_manual(name = "",
values = c("#2C77BF", "#E38072"),
labels=c(expression(atop(paste(italic("TERT"),"p-wt"),"\nn = 504")), expression(paste(italic("TERT"),"-alt"))))