如何在 paste0() 中使用 \n 或类似内容作为轴标签
How to utilize \n or similar in paste0() for axis-labels
问题: 如何利用 n\
或类似的方法在 x-axis label
中创建两个用 paste0
[=24= 编写的文本行]
我有
如您所见,我目前在 x-axis
上有两条独立的线,目前用 labels = paste0("LN: \nLND: ", paste0(seq(0,80,5))))
写的。
我希望从 seq(0,80,5)
产生的数值出现在两行中,但是 %
写在其中一个文本的后面。
预期输出:
Bonus question: is it possible to manually right/left/center align the two text lines I am trying to create on x-axis label
?
写成
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0("LN: \nLND: ", paste0(seq(0,80,5)))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death")
数据样本p
p <- structure(list(value = c(22.2, 18, 9.1, 11, 14, 15, 9, 6, 3,
25, 16, 4, 6, 10, 13, 12.1, 33, 16, 6, 9, 4.3, 23, 9, 8, 13,
5, 30, 25, 33.3, 3, 9, 33.3, 9, 12, 7, 38, 60, 5, 71.4, 7, 15,
25, 4, 16.7, 6, 15, 11.1, 9, 8, 42.9, 7, 4, 16.7, 6, 10, 10,
62.5, 8, 25, 4, 11.1, 9, 10, 14, 14, 3, 4, 50, 6, 6, 20, 33.3,
3, 7.7, 26, 13, 7.7, 13, 13, 13, 3, 7, 16.7, 6, 5, 20, 10, 15,
29, 7, 6, 11, 11.8, 17, 71.4, 14, 33.3, 18, 22, 9), os.neck = c(11.5,
11.5, 74.38, 74.38, 17.02, 7.89, 96.03, 40.48, 17.74, 14.65,
14.65, 62.46, 12.55, 9.92, 26.05, 45.47, 45.47, 17.38, 39.72,
51.45, 8.61, 8.61, 76.98, 67.09, 94.79, 72.15, 93.93, 17.05,
12.48, 12.48, 91.6, 15.87, 15.87, 11.04, 67.22, 67.02, 8.94,
8.94, 6.6, 6.6, 5.09, 10.68, 10.68, 17.15, 17.15, 0.07, 5.19,
5.19, 40.77, 0.2, 0.2, 170.88, 5.55, 5.55, 1.61, 1.61, 38.28,
38.28, 10.58, 10.58, 32.99, 32.99, 110.98, 103.69, 122.32, 14.78,
42.74, 4.04, 4.04, 8.28, 84.96, 11.7, 11.7, 49.97, 49.97, 120.48,
52.6, 52.6, 71.26, 16.3, 100.14, 55.03, 6.51, 6.51, 89.89, 51.71,
51.71, 24.97, 55.66, 21.91, 81.48, 30.92, 1.58, 1.58, 7.52, 7.52,
30.75, 30.75, 3.45, 19.22), name_new = structure(c(3L, 2L, 3L,
2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L,
3L, 2L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L,
2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 1L,
1L), .Label = c("n_fjernet0", "n_fjernet1", "lnd1"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L))
这是一个 hack,但我似乎做了你想要的 (?)。不幸的是,我认为那里挤满了所有文字的客栈看起来不太好。
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0("LN: ",paste0(seq(0,80,5)), paste0('\nLND: '), paste0(seq(0,80,5),'%'))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death") + theme(axis.text.x = element_text(size = 6,hjust = 0))
我稍微修改了你的 paste0
语句,对我来说最简单的解决方案是重复 paste0(seq(0,80,5))
以获得所需的结果:
library(ggplot2)
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0(paste0("LN: ", seq(0,80,5), "\n"), paste0("LND: ", seq(0,80,5), "%"))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death")
您可以使用 hjust
更改对齐方式,请参阅精彩解释 here。
将以下代码添加到您的 ggplot()
左对齐:
theme(axis.text.x = element_text(hjust = 0))
右对齐:
theme(axis.text.x = element_text(hjust = 1))
你们可以像这样用 paste0()
做到这一点:
myseq <- paste0(seq(0,80,5))
paste0("LN: ", myseq, "\nLND: ", paste0(myseq, "%"))
#> [1] "LN: 0\nLND: 0%" "LN: 5\nLND: 5%" "LN: 10\nLND: 10%" "LN: 15\nLND: 15%"
#> [5] "LN: 20\nLND: 20%" "LN: 25\nLND: 25%" "LN: 30\nLND: 30%" "LN: 35\nLND: 35%"
#> [9] "LN: 40\nLND: 40%" "LN: 45\nLND: 45%" "LN: 50\nLND: 50%" "LN: 55\nLND: 55%"
#> [13] "LN: 60\nLND: 60%" "LN: 65\nLND: 65%" "LN: 70\nLND: 70%" "LN: 75\nLND: 75%"
#> [17] "LN: 80\nLND: 80%"
由 reprex package (v0.3.0)
于 2020-06-07 创建
但更优雅的解决方案是 sprintf()
。结果与更少的代码相同。您可以给 sprintf()
一个输入并多次使用它:
sprintf("LN: %1$s\nLND: %1$s%%", seq(0,80,5))
#> [1] "LN: 0\nLND: 0%" "LN: 5\nLND: 5%" "LN: 10\nLND: 10%" "LN: 15\nLND: 15%"
#> [5] "LN: 20\nLND: 20%" "LN: 25\nLND: 25%" "LN: 30\nLND: 30%" "LN: 35\nLND: 35%"
#> [9] "LN: 40\nLND: 40%" "LN: 45\nLND: 45%" "LN: 50\nLND: 50%" "LN: 55\nLND: 55%"
#> [13] "LN: 60\nLND: 60%" "LN: 65\nLND: 65%" "LN: 70\nLND: 70%" "LN: 75\nLND: 75%"
#> [17] "LN: 80\nLND: 80%"
由 reprex package (v0.3.0)
于 2020-06-07 创建
问题: 如何利用 n\
或类似的方法在 x-axis label
中创建两个用 paste0
[=24= 编写的文本行]
我有
如您所见,我目前在 x-axis
上有两条独立的线,目前用 labels = paste0("LN: \nLND: ", paste0(seq(0,80,5))))
写的。
我希望从 seq(0,80,5)
产生的数值出现在两行中,但是 %
写在其中一个文本的后面。
预期输出:
Bonus question: is it possible to manually right/left/center align the two text lines I am trying to create on
x-axis label
?
写成
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0("LN: \nLND: ", paste0(seq(0,80,5)))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death")
数据样本p
p <- structure(list(value = c(22.2, 18, 9.1, 11, 14, 15, 9, 6, 3,
25, 16, 4, 6, 10, 13, 12.1, 33, 16, 6, 9, 4.3, 23, 9, 8, 13,
5, 30, 25, 33.3, 3, 9, 33.3, 9, 12, 7, 38, 60, 5, 71.4, 7, 15,
25, 4, 16.7, 6, 15, 11.1, 9, 8, 42.9, 7, 4, 16.7, 6, 10, 10,
62.5, 8, 25, 4, 11.1, 9, 10, 14, 14, 3, 4, 50, 6, 6, 20, 33.3,
3, 7.7, 26, 13, 7.7, 13, 13, 13, 3, 7, 16.7, 6, 5, 20, 10, 15,
29, 7, 6, 11, 11.8, 17, 71.4, 14, 33.3, 18, 22, 9), os.neck = c(11.5,
11.5, 74.38, 74.38, 17.02, 7.89, 96.03, 40.48, 17.74, 14.65,
14.65, 62.46, 12.55, 9.92, 26.05, 45.47, 45.47, 17.38, 39.72,
51.45, 8.61, 8.61, 76.98, 67.09, 94.79, 72.15, 93.93, 17.05,
12.48, 12.48, 91.6, 15.87, 15.87, 11.04, 67.22, 67.02, 8.94,
8.94, 6.6, 6.6, 5.09, 10.68, 10.68, 17.15, 17.15, 0.07, 5.19,
5.19, 40.77, 0.2, 0.2, 170.88, 5.55, 5.55, 1.61, 1.61, 38.28,
38.28, 10.58, 10.58, 32.99, 32.99, 110.98, 103.69, 122.32, 14.78,
42.74, 4.04, 4.04, 8.28, 84.96, 11.7, 11.7, 49.97, 49.97, 120.48,
52.6, 52.6, 71.26, 16.3, 100.14, 55.03, 6.51, 6.51, 89.89, 51.71,
51.71, 24.97, 55.66, 21.91, 81.48, 30.92, 1.58, 1.58, 7.52, 7.52,
30.75, 30.75, 3.45, 19.22), name_new = structure(c(3L, 2L, 3L,
2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L,
3L, 2L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L,
2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 1L,
1L), .Label = c("n_fjernet0", "n_fjernet1", "lnd1"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -100L))
这是一个 hack,但我似乎做了你想要的 (?)。不幸的是,我认为那里挤满了所有文字的客栈看起来不太好。
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0("LN: ",paste0(seq(0,80,5)), paste0('\nLND: '), paste0(seq(0,80,5),'%'))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death") + theme(axis.text.x = element_text(size = 6,hjust = 0))
我稍微修改了你的 paste0
语句,对我来说最简单的解决方案是重复 paste0(seq(0,80,5))
以获得所需的结果:
library(ggplot2)
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) +
geom_point(size=2, shape=21, stroke=1, fill="white") +
geom_quantile(quantiles=.5, size=1.3) +
scale_x_continuous(breaks = seq(0,80,5),
name="Lymph nodal yield",
labels = paste0(paste0("LN: ", seq(0,80,5), "\n"), paste0("LND: ", seq(0,80,5), "%"))) +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death")
您可以使用 hjust
更改对齐方式,请参阅精彩解释 here。
将以下代码添加到您的 ggplot()
左对齐:
theme(axis.text.x = element_text(hjust = 0))
右对齐:
theme(axis.text.x = element_text(hjust = 1))
你们可以像这样用 paste0()
做到这一点:
myseq <- paste0(seq(0,80,5))
paste0("LN: ", myseq, "\nLND: ", paste0(myseq, "%"))
#> [1] "LN: 0\nLND: 0%" "LN: 5\nLND: 5%" "LN: 10\nLND: 10%" "LN: 15\nLND: 15%"
#> [5] "LN: 20\nLND: 20%" "LN: 25\nLND: 25%" "LN: 30\nLND: 30%" "LN: 35\nLND: 35%"
#> [9] "LN: 40\nLND: 40%" "LN: 45\nLND: 45%" "LN: 50\nLND: 50%" "LN: 55\nLND: 55%"
#> [13] "LN: 60\nLND: 60%" "LN: 65\nLND: 65%" "LN: 70\nLND: 70%" "LN: 75\nLND: 75%"
#> [17] "LN: 80\nLND: 80%"
由 reprex package (v0.3.0)
于 2020-06-07 创建但更优雅的解决方案是 sprintf()
。结果与更少的代码相同。您可以给 sprintf()
一个输入并多次使用它:
sprintf("LN: %1$s\nLND: %1$s%%", seq(0,80,5))
#> [1] "LN: 0\nLND: 0%" "LN: 5\nLND: 5%" "LN: 10\nLND: 10%" "LN: 15\nLND: 15%"
#> [5] "LN: 20\nLND: 20%" "LN: 25\nLND: 25%" "LN: 30\nLND: 30%" "LN: 35\nLND: 35%"
#> [9] "LN: 40\nLND: 40%" "LN: 45\nLND: 45%" "LN: 50\nLND: 50%" "LN: 55\nLND: 55%"
#> [13] "LN: 60\nLND: 60%" "LN: 65\nLND: 65%" "LN: 70\nLND: 70%" "LN: 75\nLND: 75%"
#> [17] "LN: 80\nLND: 80%"
由 reprex package (v0.3.0)
于 2020-06-07 创建