无法使用cowplot包左对齐

3
在当前的图中,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' )


职威,这个问题只在那个栅格化的 flex_table 对象中出现吗? - tjebo
@tjebo,是的,我认为如果p2是一个普通的ggplot对象,这种情况不会发生。 - zhiwei li
1个回答

1
问题在于栅格对象位于图形的正中央。请尝试使用 ggplot() + annotation_custom(rasterGrob(ft_raster)) 命令,不要加上 theme_void。您可以调整栅格的坐标(顺便说一下,“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) )

2022年6月8日由reprex包(v2.0.1)创建


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接