在当前图中,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_void
的 ggplot() + 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/