gpt4 book ai didi

r - 如何在 R 中的 tableGrob() 下对齐 ggparagraph() 文本

转载 作者:行者123 更新时间:2023-12-04 04:23:24 28 4
gpt4 key购买 nike

我想要实现的目标

我正在尝试在 ggparagraph() 中实现边距,但找不到任何有帮助的设置。我尝试在 ggarrange() 中将两个宽度设置为相同,但这没有用,这是一种解决方法。当我从 ggarrange() 输出导出 PDF 时,现在会发生的情况是文本与 PDF 页面的宽度一样长。 enter image description here我不想改变 PDF 页面的宽度。

代码

这里是一些示例代码:

text <- paste("iris data set gives the measurements in cm",
"of the variables sepal length and width",
"and petal length and width, respectively,",
"for 50 flowers from each of 3 species of iris.",
"The species are Iris setosa, versicolor, and virginica.", sep = " ")
text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black")
stable <- desc_statby(iris, measure.var = "Sepal.Length",
grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- tableGrob(stable, rows = NULL,
theme = ttheme("mOrange"))

ggarrange(stable.p, text.p,
ncol = 1, nrow = 2,
heights = c(1, 0.5))

我根据这个 source 稍微修改了代码满足我的需求。

提前致谢!

最佳答案

您可以使用 theme(plot.margin = )。您可以修改对 units() 的调用中的数字以获取您感兴趣的边距。请注意 trb, l分别代表top, right, bottom, left。

library(gridExtra)
library(ggpubr)
text <- paste("iris data set gives the measurements in cm",
"of the variables sepal length and width",
"and petal length and width, respectively,",
"for 50 flowers from each of 3 species of iris.",
"The species are Iris setosa, versicolor, and virginica.", sep = " ")

text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black") +
theme(plot.margin = unit(c(t = 0, r = 12, b = 3, l = 12),"lines"))

stable <- desc_statby(iris, measure.var = "Sepal.Length",
grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- tableGrob(stable, rows = NULL,
theme = ttheme("mOrange"))

ggarrange(stable.p, text.p, ncol = 1, nrow = 2, heights = c(1, 0.5))

enter image description here

关于r - 如何在 R 中的 tableGrob() 下对齐 ggparagraph() 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58542332/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com