Question
如何得到paste
and parse
in annotate
of ggplot2
尊重换行符(\n)?
问题和MWE
我正在尝试重现ggplot2
使用 NMDS 分析的应力图metaMDS
包装内vegan.
这是我的 MWE,后面是结果图。
library(ggplot2)
library(tibble)
library(vegan)
set.seed(42) # for reproducibility
data(dune)
fit <- metaMDS(dune)
tib <- tibble(fit$diss, fit$dist, fit$dhat)
colnames(tib) <- c("diss", "dist", "dhat")
stress <- fit$stress
coord_x <- min(tib$diss)
coord_y <- max(tib$dist)
nonmetric_r2 <- round(1 - stress * stress, digits = 3)
linear_r2 <- round(summary(lm(fit$dist~fit$dhat))$adj.r.squared, 3)
## How do I get the newline character to be honored?
nonmetric_label = paste0("Non-metric~fit~italic(R)^2 ==", nonmetric_r2, "~\n Linear~fit~italic(R)^2 ==", linear_r2)
ggplot(tib,
aes(x = diss, y = dist)) +
geom_point(color = "blue") +
geom_line(aes(x = diss, y = dhat), color = "red") +
annotate(
geom = "text",
x = coord_x,
y = coord_y,
hjust = 0,
#vjust = 1,
label = nonmetric_label, parse = TRUE) +
labs(x = "Observed Dissimilarity",
y = "Ordination Distance")
上面的单个注释行应该位于两个单独的行上,如下所示(来自stressplot(fit)
).
违规行是
nonmetric_label = paste0("Non-metric~fit~italic(R)^2 ==", nonmetric_r2, "~\n Linear~fit~italic(R)^2 ==", linear_r2)
如果我之前不包含波浪号\n
,那么换行符之后的所有内容都会消失。我尝试过波浪号放置和放置的各种组合'\n~Linear~fit'
在额外的单引号和反引号中。
如何使所需的注释显示在两行上?