Seb*_*uer 5 latex r line-breaks ggplot2 plotmath
情况:
我有一个ggplot图,我想在其中添加一些文本注释.文本注释应该出现在两行中(可读性和空间),每行包含一些TeX公式:
library(tidyverse)
library(latex2exp)
ggplot(NULL, aes(c(-5,5))) +
geom_area(stat = "function", fun = dnorm, fill = "grey40", xlim = c(-2, 2)) +
annotate(geom = "text", label = TeX(paste("Distribution of $\\bar{x}$","\n","under $H_0$")),
x = -1, y = 0.3,
color = "red")
Run Code Online (Sandbox Code Playgroud)
问题:
换行没有显示出来.该线不分为两行.
什么没有奏效:
我曾尝试paste(TeX(...)),并parse = T没有成功.
我也试过这个label = expression(paste("distribution of ", bar(x), "\n", "under H0"))抬头这里,没有成功.
题:
如何将注释(红色文本)分成两行?
你可以使用atop和plotmath表达式(参见?plotmath更多信息):
ggplot(NULL, aes(c(-5,5))) +
geom_area(stat = "function", fun = dnorm, fill = "grey70", xlim = c(-2, 2)) +
annotate(geom = "text", label = expression(atop("Distribution of"~bar(x), "under"~H[0])),
x = -1, y = 0.3,
color = "red") +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
我已经更改了此示例的主题和颜色,以便文本突出显示.
更新:关于评论,这是一个选项,虽然你需要玩垂直间距.我们首先构造exp一个plotmath表达式列表.然后,在annotate,我们需要y作为一个值的向量,其长度等于中的元素数exp.parse=TRUE告诉ggplot治疗的元素exp作为plotmath表现形式和解析它们:
exp = list("Distribution of"~bar(x),
"under"~H[0],
hat(mu)~"is the mean")
ggplot(NULL, aes(c(-5,5))) +
geom_area(stat = "function", fun = dnorm, fill = "grey70", xlim = c(-2, 2)) +
annotate(geom = "text", label = exp,
x = -1, y = seq(0.32,0.28,length=3),
size=3, color = "red", parse=TRUE) +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1852 次 |
| 最近记录: |