dismantling a ggplot with grid and gtable

前端 未结 1 677
Happy的楠姐
Happy的楠姐 2021-01-15 17:02

I\'m struggling to build a dual-axis plot based on ggplot objects. At baptiste\'s suggestion, I have broken down the problem into smaller parts. The present iss

1条回答
  •  囚心锁ツ
    2021-01-15 17:24

    A more natural strategy would be to use invisible geom_blank layers, so that ggplot2 still trains the scales etc to build the plot but shows no data. Since you want to process already-formatted plots, however, you probably have to remove manually those grobs from the plot gTree. Here's an attempt,

    library(gtable)
    g <- ggplotGrob(p)
    
    stripdata <- function(g){
      keep <- grepl("border|grill", 
                    names(g[["grobs"]][[4]][["children"]]))
      g[["grobs"]][[4]][["children"]][!keep] <- NULL
      g
    }
    
    grid.newpage()
    grid.draw(stripdata(g))
    

    0 讨论(0)
提交回复
热议问题