R: function to scale ggplot2, lattice or base R graph proportionally

后端 未结 2 2142
一个人的身影
一个人的身影 2021-02-08 02:13

In R I always find it annoying that base R, lattice and ggplot2 plots all work with absolute point sizes for the size of text and plot symbols.

This means that if you in

2条回答
  •  无人共我
    2021-02-08 02:29

    for some reason the point size is encoded as fontsize, so it doesn't seem easy to convert it to units that would scale automatically. If you know what scaling you'd want to apply, then the following might help

    library(ggplot2)
    p <- qplot(Sepal.Length, Petal.Length, data = iris, 
               geom=c("point","line"),
               color = Species, size = Petal.Width, alpha = I(0.7))
    
    library(gtable)
    library(grid)
    
    g <- ggplotGrob(p)
    pts <- g$grobs[[4]][["children"]][[2]] # geom_point layer
    lns <- g$grobs[[4]][["children"]][[3]] # geom_line layer
    g$grobs[[4]][["children"]][[2]] <- editGrob(pts, size=1.2*pts$size)
    gp <- modifyList(lns$gp, list(lwd=1.2*lns$gp$lwd))
    g$grobs[[4]][["children"]][[3]] <- editGrob(lns, gp=gp)
    
    grid.newpage()
    grid.draw(g)
    

提交回复
热议问题