将基准图转换为grob,保持纵横比

将基准图转换为grob,保持纵横比,r,ggplot2,cowplot,grob,R,Ggplot2,Cowplot,Grob,我需要将R基本图转换为grob,这样它就可以叠加在一些ggplots上 我发现有两个函数可以实现这一点,ggplotify::as.grob和cowplot::plot_to_gtable。问题是,它们没有保留原始基准图的纵横比。由于所讨论的基本图是用Circize包绘制的一个圆,因此我需要保留纵横比,否则就不可能在GG图上一致地叠加 下面是一些示例代码来说明我在做什么: library(circlize) library(cowplot) tst <- function() { d

我需要将R基本图转换为grob,这样它就可以叠加在一些
ggplot
s上

我发现有两个函数可以实现这一点,
ggplotify::as.grob
cowplot::plot_to_gtable
。问题是,它们没有保留原始基准图的纵横比。由于所讨论的基本图是用
Circize
包绘制的一个圆,因此我需要保留纵横比,否则就不可能在GG图上一致地叠加

下面是一些示例代码来说明我在做什么:

library(circlize)
library(cowplot)

tst <- function() {
  df <- data.frame(
    sector = factor(letters), 
    label = letters
  )
  circos.clear()
  circos.initialize(df$sector, xlim=c(-1.0, 1.0), sector.width=1)
  circos.trackPlotRegion(factors=df$sector,
                         y=rep(1.0, length(df$sector)),
                         ylim=c(0, 1.0))

  circos.trackText(df$sector, 
                   x=rep(0, nrow(df)), y=rep(0, nrow(df)),
                   facing="bending", niceFacing = T,
                   labels=df$label)
}

# Run tst() now and see a nice circle
tst()
# If you resize your view window, it will always be redrawn as a circle

agrob <- cowplot::plot_to_gtable(tst)
ggdraw(agrob)
# But this produces an oval, that is redrawn to different proportions when the window is resized

plt <- data.frame(group = c('a', 'b', 'c'), sizes = c(.3, .4, .3)) %>%
   ggplot(aes(x=group, y = sizes, fill=group)) +
   geom_bar(stat='identity', width=1) + 
   coord_polar("x") +
   guides(fill=FALSE)


ggdraw(plt) + draw_plot(agrob)
# And here you see the problem in superimposing the circle over the ggplot
库(圆圈)
图书馆(cowplot)

tst这在cowplot的开发版本中得到了解决。如果要混合使用基本图形和栅格图形,则应更新

库(圆圈)
库(cowplot)#开发工具::安装github(“wilkelab/cowplot”)
图书馆(dplyr)
图书馆(GG2)

是的,修正了!谢谢我注意到他们仍然有点偏离中心,但有很大的改进。