4

I need to convert an R base plot to a grob, so it can be superimposed over some ggplots.

There are a couple of functions for doing this that I've found, ggplotify::as.grob and cowplot::plot_to_gtable. The trouble is, they don't preserve the aspect ratio of the original base plot. Since the base plot in question is a circle drawn with the circlize package, I need to preserve the aspect ratio, otherwise its impossible to consistently superimpose over the ggplots.

Here's some example code to show what I'm doing:

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

Can anyone help? Thanks!

Bob
  • 1,274
  • 1
  • 13
  • 26

1 Answers1

2

This is resolved in the development version of cowplot. If you want to mix base graphics and grid graphics, you should update.

library(circlize)
library(cowplot) # devtools::install_github("wilkelab/cowplot")
library(dplyr)
library(ggplot2)

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()

# cowplot::as_grob() produces the exact same result

agrob <- cowplot::as_grob(tst)
ggdraw(agrob)

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)

Created on 2018-10-30 by the reprex package (v0.2.1)

Tung
  • 26,371
  • 7
  • 91
  • 115
Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Yup - fixed! Thanks! I notice they're slightly off-center still, but a vast improvement. – Bob Oct 31 '18 at 23:09