R:带多行文字的直方图字幕:如何在列中获得表格对齐?
Posted
技术标签:
【中文标题】R:带多行文字的直方图字幕:如何在列中获得表格对齐?【英文标题】:R: Histogram subtitle with mtext: how to get tabular alignment in columns? 【发布时间】:2017-09-01 21:01:29 【问题描述】:我有一个带有多行字幕的情节:
文本的当前代码:
bq1 <- bquote("Raw: "
~ mu
~ "="
~ .(format(round(mean(stepsperday$steps), digits=2), big.mark=","))
~ ", "
~ sigma
~ "="
~ .(format(round(sd(stepsperday$steps), digits=2), big.mark = ","))
~ ", median ="
~ .(format(median(stepsperday$steps), big.mark = ","))
)
bq2 <- bquote("Imputed: "
~ mu
~ "="
~ .(format(round(mean(stepsperday.imputed$steps), digits=2), big.mark=","))
~ ", "
~ sigma
~ "="
~ .(format(round(sd(stepsperday.imputed$steps), digits=2), big.mark = ",", digits = 6))
~ ", median ="
~ .(format(median(stepsperday.imputed$steps), big.mark = ","))
)
mytext <- list(bq2, bq1)
mtext(do.call(expression, mytext),side=3,line=0:1, cex=.75)
我希望 2 行在相应的数据位上水平对齐 - 平均值、标准差、中位数,就像它们在表格中一样。
有什么办法吗?
感谢任何提示,
sff
【问题讨论】:
我在bq1
中的“Raw”前面放了7个空格,得到了很好的对齐效果
调用 mtext 两次,一次用于raw\nImputed
,一次用于其余的,使用不同的 adj
感谢@RichardTelford - 按照您的建议逐列执行 mtext,按照 g5w 的建议策略性地放置空格。希望有一个更“结构化”的解决方案,但是哦。
【参考方案1】:
根据 cmets,以下内容就足够了:
data1 <- " Raw:"
data2 <- "Imputed:"
mytext1 <- list(data2, data1)
mtext(do.call(expression, mytext1), side=3, line=0:1, adj=c(.23, .23), cex=.75)
bq1 <- bquote(" " ~ mu
~ "="
~ .(format(round(mean(stepsperday$steps), digits=2), big.mark=","))
~ " "
~ sigma
~ "="
~ .(format(round(sd(stepsperday$steps), digits=2), big.mark = ","))
~ " median ="
~ .(format(median(stepsperday$steps), big.mark = ","))
)
bq2 <- bquote(" " ~ mu
~ "="
~ .(format(round(mean(stepsperday.imputed$steps), digits=2), big.mark=","))
~ " "
~ sigma
~ "="
~ .(format(round(sd(stepsperday.imputed$steps), digits=2), big.mark = ",", digits = 6))
~ " median ="
~ .(format(median(stepsperday.imputed$steps), big.mark = ","))
)
mytext <- list(bq2, bq1)
mtext(do.call(expression, mytext), side=3, line=0:1, adj=c(.6, .6), cex=.75)
产生输出:
【讨论】:
以上是关于R:带多行文字的直方图字幕:如何在列中获得表格对齐?的主要内容,如果未能解决你的问题,请参考以下文章