ggplot and axis numbers and labels

后端 未结 2 1564
[愿得一人]
[愿得一人] 2020-12-18 14:39

i tried to build the following sample plot (see here for an example of a CIELAB Color Wheel). It is nearly finished, but there are still some small issues (see demo): - the

2条回答
  •  悲哀的现实
    2020-12-18 14:54

    moving the whole axis inside the plot panel is not straight-forward (arguably for good reasons). You can do it like this,

    g <- ggplotGrob(p)
    
    library(gtable)
    # move the axis up in the gtable
    g$layout[g$layout$name == "axis-b", c("t", "b")] <- 
      g$layout[g$layout$name == "panel", c("t", "b")] 
    # extract the axis gTree and modify its viewport 
    a <- g$grobs[[which(g$layout$name == "axis-b")]] 
    a$vp <- modifyList(a$vp, list(y=unit(0.5, "npc")))
    g$grobs[[which(g$layout$name == "axis-b")]] <- a
    
    
    g$layout[g$layout$name == "axis-l", c("l", "r")] <- 
      g$layout[g$layout$name == "panel", c("l", "r")] 
    # extract the axis gTree and modify its viewport 
    b <- g$grobs[[which(g$layout$name == "axis-l")]] 
    b$vp <- modifyList(b$vp, list(x=unit(0.5, "npc")))
    g$grobs[[which(g$layout$name == "axis-l")]] <- b
    
    #grid.newpage()
    #grid.draw(g)
    
    
    ## remove all cells but panel
    panel <- g$layout[g$layout$name == "panel",] 
    
    gtrim <- g[panel$b, panel$r]
    ## add new stuff
    gtrim <- gtable_add_rows(gtrim, unit(1,"line"), pos=0)
    gtrim <- gtable_add_rows(gtrim, unit(1,"line"), pos=-1)
    gtrim <- gtable_add_cols(gtrim, unit(1,"line"), pos=0)
    gtrim <- gtable_add_cols(gtrim, unit(1,"line"), pos=-1)
    
    gtrim <- gtable_add_grob(gtrim, list(textGrob("top"),
                                         textGrob("left", rot=90),
                                         textGrob("right", rot=90),
                                         textGrob("bottom")), 
                             t=c(1,2,2,3),
                             l=c(2,1,3,2))
    
    grid.newpage()
    grid.draw(gtrim)
    

    enter image description here

提交回复
热议问题