Make a rectangular legend, with rows and columns labeled, in grid

前端 未结 2 1000
梦谈多话
梦谈多话 2020-12-01 11:10

I\'ve got a ggplot where I\'m mapping factors to both fill and alpha, like this:

set.seed(47)
the_data <- data.frame(value = rpois(6, lambda=20),
                 


        
2条回答
  •  眼角桃花
    2020-12-01 11:37

    @Henrik

    This might be a little easier,

    g1 <- ggplotGrob(p1)
    g2 <- ggplotGrob(p2)
    
    leg1 <- gtable_filter(g1, "guide-box") 
    leg2 <- gtable_filter(g2, "guide-box") 
    leg <- gtable:::cbind_gtable(leg1[["grobs"]][[1]],  leg2[["grobs"]][[1]], "first")
    
    g1$grobs[g1$layout$name == "guide-box"][[1]] <- leg
    g1$widths[max(subset(g1$layout, name == "guide-box")[["r"]])] <- list(leg1$width + leg2$width)
    
    grid.newpage()
    grid.draw(g1)
    

提交回复
热议问题