How to place grobs with annotation_custom() at precise areas of the plot region?

前端 未结 2 1517
别那么骄傲
别那么骄傲 2020-11-27 15:33

I am trying to reproduce the following [base R] plot with ggplot2

\"enter

2条回答
  •  青春惊慌失措
    2020-11-27 16:27

    Maybe this can illustrate annotation_custom,

    myGrob <- grobTree(rectGrob(gp=gpar(fill="red", alpha=0.5)),
                       segmentsGrob(x0=0, x1=1, y0=0, y1=1, default.units="npc"))
    
    myGrob2 <- grobTree(rectGrob(gp=gpar(fill="blue", alpha=0.5)),
                       segmentsGrob(x0=0, x1=1, y0=0, y1=1, default.units="npc"))
    
    p <- qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) +
      annotation_custom(myGrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) +
      annotate("segment", x=5, xend=6, y=3, yend=5, colour="red") +
      annotation_custom(myGrob2, xmin=8, xmax=12, ymin=3.5, ymax=5.5) 
    
    p
    
    g <- ggplotGrob(p)
    g$layout$clip[g$layout$name=="panel"] <- "off"
    grid.draw(g)
    

    enter image description here

    There's a weird bug apparently, whereby if I reuse myGrob instead of myGrob2, it ignores the placement coordinates the second time and stacks it up with the first layer. This function is really buggy.

提交回复
热议问题