r - 不能与 cowplot 包左对齐

标签 r ggplot2 flextable cowplot

在当前图中,p2 现在居中。我希望 p2 与左侧的 p1 对齐。我尝试了 plot_grid 中的一些参数,但图像根本没有改变。

library(ggplot2)
library(flextable)
library(grid)
library(cowplot)
library(tidyverse)

mydf <- tibble(a = c(1,2,3,4,5,4),
               b = c(4,4,4,3,3,3))

p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()

ft_raster <- mydf %>% flextable::flextable() %>% 
  as_raster()

p2 <- ggplot() + 
  theme_void() + 
  annotation_custom(rasterGrob(ft_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)

# orginal plot
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(1, 1) )

# left align plot (Nothing changed with orginal plot)
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(1, 1), align = 'v' )
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(1, 1), axis = 'l' )
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(1, 1), axis = 'tblr' )

最佳答案

问题是光栅对象被放置在绘图的中间。检查没有 theme_voidggplot() + annotation_custom(rasterGrob(ft_raster))。您可以使用光栅的坐标(顺便说一下,Inf 是默认值...

library(flextable)
library(grid)
library(cowplot)
library(tidyverse)

mydf <- tibble(a = c(1,2,3,4,5,4),
               b = c(4,4,4,3,3,3))

p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()

ft_raster <- mydf %>% 
  flextable::flextable() %>% 
  as_raster()

p2 <-
  ggplot() + 
  theme_void() +
# play around with this !
  annotation_custom(rasterGrob(ft_raster), xmax = 0.2)

cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(1, 1) )

reprex package 创建于 2022-06-08 (v2.0.1)

关于r - 不能与 cowplot 包左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72540050/

相关文章:

r - 缩进使用 R-ReporteRs 制作的 Flextable

r - 从flextable中的字符串动态设置colnames

gwt - GWT HTMLTable 中的 getCell(row,col)

r - 是否可以让RStudio显示自定义函数的函数参数和描述?

string - 将字符串转换为 aes_string 的美学

根据时间戳顺序删除重复项

r - 将 exp/power 趋势线添加到 ggplot

r - 如何用ggplot2中一列的总和做直方图?

正则表达式匹配特定字符的确切数量

r - data.frame 和变量类 R 的变化