Eng*_*IRD 6 parsing text newline r ggplot2
我试图在 ggplot2 的轴标题中插入一个有意的换行符。正如下面的 MWE 所示,我用来parse(text=X)评估LaTeX希腊字符和下标/上标的可能样式包含。我已经看到 ggpot2 和表达式对象的标签工具应该能够处理新行字符,例如\n. 但我致力于使用parse(text=X). 然而,我没有成功地将其合并到我的 x 和 y 轴标题字符串中。
\n.*{"\n"}*,我得到与*. 这让我怀疑它parse(text=X)不支持新行符号。如果是这种情况,插入新行的适当方法是什么?
过去我已经能够使用,atop但这次更喜欢尝试使用不同的东西,因为它在行之间留下了太多的空白。因此,或者,我会接受一个演示如何自定义 的间距的答案atop。
微量元素:
library(ggplot2)
library("grid")
print("Program started")
gg <- ggplot(diamonds, aes(clarity, fill=cut))
gg <- gg + geom_bar(position="dodge")
gg <- gg + labs(y=parse(text="ahhhh\nWe~are~here"), x=parse(text="ahhhh\nWe~are~here"),title="title")
gg <- gg + theme(
legend.justification=c(0,1),
legend.position=c(0,1),
legend.title=element_blank(),
plot.background=element_rect(fill="red")
);
gg <- gg + guides(fill=guide_legend(title.position="top"))
print(gg)
print("Program complete - a graph should be visible.")
Run Code Online (Sandbox Code Playgroud)
将文本用双引号内的附加单引号引起来,您将在轴上获得两行标签。
gg + labs(y=parse(text="'ahhhh\nWe~are~here'"), x=parse(text="'ahhhh\nWe~are~here'"),title="title")
您可以使用单引号字符串和表达式的组合来构建绘图输出,但有一些限制 - 例如,您的单引号字符串不能以\n
检查此代码
parse(text = "'ahhhh\nWe' ~are%=>% bold(here[123])") # it works
parse(text = "'ahhhh\n' We~are%=>% bold(here[123])") # it throws an error
Run Code Online (Sandbox Code Playgroud)