【问题标题】:How to plot an aligned text on severals lines / colomns?如何在多行/列上绘制对齐的文本?
【发布时间】:2019-10-21 18:51:59
【问题描述】:

我正在通过几行和几列对齐文本(左/右/上/下)创建一个“页面”。我想使用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"))
)

显示效果不错:

但是,如果我将绘图保存在 pdf 文件中,结果是不对齐的

save_plot("myPlot.pdf", myPlot, nrow=3, ncol=2)

结果与预期不符

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

【问题讨论】:

  • 您是否尝试使用plot_gridalign 参数?
  • 嗨,是的,我尝试了所有对齐参数(“none”、“h”、“v”和“hv”)但没有成功

标签: r ggplot2 r-grid cowplot


【解决方案1】:

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

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 行才真正被绘制。如果您在 R / RStudio 的图形设备中绘制了myPlot,它看起来与您以 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"))

如果您想将已经对齐的文本块叠加在一起,则根本不应该使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多