【发布时间】:2017-11-05 17:04:36
【问题描述】:
问题
我如何在ggplot2 的annotate 中获得paste 和parse 以兑现换行符(\n)?
问题和 MWE
我正在尝试在 ggplot2 中使用 metaMDS 在包 vegan. 中重现 NMDS 分析的压力图这是我的 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' 放在额外的单引号和反引号中。
如何让所需的注释出现在两行上?
【问题讨论】:
-
plotmath 不支持换行符。也许在上面就足够了?请参阅帮助(“plotmath”)。