gpt4 book ai didi

r - 如何在多行/列上绘制对齐的文本?

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

我正在通过几行和几列对齐文本(左/右/上/下)创建一个“页面”。我想使用 grid.arrange()功能,但我做不到。我在一篇旧帖子中读到 grid_plot()功能完成这项工作。

所以我的代码是

# Libraries
library(ggplot2)
library(grid)
library(cowplot)

x <- unit(1:3/(3+1), "npc")
y <- unit(1:2/(2+1), "npc")
grid.grill(h=y, v=x, gp=gpar(col="grey"))

myPlot <- plot_grid(
grid.text(label="Information:", x=x[1], y=y[2], just=c("left", "bottom"), gp=gpar(fontface = "bold", fontsize = 15, col = "black")),
grid.text(label="Name:", x=x[2], y=y[1], just=c("right", "bottom"), gp=gpar(fontface = "plain", fontsize = 13, col = "red")),
grid.text(label="John Doe ", x=x[2], y=y[1], just=c("left", "bottom"), gp=gpar(fontface = "plain", fontsize = 13, col = "blue"))
)

显示结果很好:
Aligned plot

但是,如果我将绘图保存在 pdf 文件中,结果是不对齐的
save_plot("myPlot.pdf", myPlot, nrow=3, ncol=2)

结果并不如预期
No aligned plot

我的问题是:如何对齐 pdf 文件中的文本?

最佳答案

您在第一种情况下显示的结果是 不是 plot_grid 的结果.发生的事情是 grid.text函数(与 textGrob 不同)默认绘制创建的文本 grob,因此三个文本 grob 中的每一个都在同一个 Grid 视口(viewport)中相互叠加绘制。从视口(viewport)的角度来看,发生的事情相当于以下内容:

grid.grill(h=y, v=x, gp=gpar(col="grey"))
grid.text(label="Information:", x=x[1], y=y[2], just=c("left", "bottom"), gp=gpar(fontface = "bold", fontsize = 15, col = "black"))
grid.text(label="Name:", x=x[2], y=y[1], just=c("right", "bottom"), gp=gpar(fontface = "plain", fontsize = 13, col = "red"))
grid.text(label="John Doe ", x=x[2], y=y[1], just=c("left", "bottom"), gp=gpar(fontface = "plain", fontsize = 13, col = "blue"))

同时, plot_grid函数获取创建的文本 grobs,根据 2-row-2-column 排列它们,并将结果分配给 myPlot .在您的原始代码中, myPlot直到 save_plot 才真正绘制线。你画了吗 myPlot在 R/RStudio 的图形设备中,它看起来与您以 pdf 形式获得的相同。乍一看似乎未对齐的文本实际上完全按照预期对齐 - 一旦我们考虑到这些实际上是并排的图,而不是重叠的图:
myPlot
grid.grill(h = unit(1:5/6, "npc"), v = unit(1:7/8, "npc"), gp = gpar(col = "grey"))
grid.grill(h = unit(1/2, "npc"), v = unit(1/2, "npc"), gp = gpar(col = "black"))

enter image description here

如果你想将已经对齐的文本 block 叠加在一起,你不应该使用 plot_grid一点也不。 cowplot 包中的低级函数将更好地满足您的目的:
# this is a matter of personal preference, but I generally find it less confusing to
# keep grob creation separate from complex cases of grob drawing / arranging.
gt1 <- grid.text(label="Information:", x=x[1], y=y[2], just=c("left", "bottom"),
gp=gpar(fontface = "bold", fontsize = 15, col = "black"))
gt2 <- grid.text(label="Name:", x=x[2], y=y[1], just=c("right", "bottom"),
gp=gpar(fontface = "plain", fontsize = 13, col = "red"))
gt3 <- grid.text(label="John Doe ", x=x[2], y=y[1], just=c("left", "bottom"),
gp=gpar(fontface = "plain", fontsize = 13, col = "blue"))

# ggdraw() & draw_plot() fill up the entire plot by default, so the results are overlaid.
myPlot <- ggdraw(gt1) + draw_plot(gt2) + draw_plot(gt3)
myPlot # show in default graphics device to check alignment
save_plot("myPlot.pdf", myPlot) # save as pdf

enter image description here

关于r - 如何在多行/列上绘制对齐的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56474414/

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