R:函数按比例缩放ggplot2,格或基R图 [英] R: function to scale ggplot2, lattice or base R graph proportionally

查看:502
本文介绍了R:函数按比例缩放ggplot2,格或基R图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这意味着在RI中,总是发现令人讨厌的是,基R,lattice和ggplot2绘制了所有与绝对点大小一起工作的文本和图形符号的大小。如果你增加说你的情节窗口的大小来获取一个页面填充图

  windows(width = 5,height = 5); qplot(Sepal.Length,Petal.Length,data = iris,color = Species,size = Petal.Width,alpha = I(0.7))

to

  windows(width = 10,height = 10); qplot (Sepal.Length,Petal.Length,data = iris,color = Species,size = Petal.Width,alpha = I(0.7))

表示文字和图表符号的相对比例相对于其余部分的变化。
这同样适用于基R和格图。


在我写的一些图导出函数的上下文中,我想知道是否可以写一个函数,可以使用当前图形设备的输出(可以使用 recordPlot())来访问它,并且通过一个缩放比例来缩放所有绘图符号和文本以及线宽任意%,从而允许图表以任意大小导出,同时仍保持相同的外观?即一个函数按比例将整个图形缩放到更大或更小的尺寸,同时保持外观相同?

PS


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 increase say the size of your plot window to get a page-filling graph from

windows(width=5,height=5);qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))

to

windows(width=10,height=10);qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))

that the relative scaling of the text and plot symbols change relative the rest. The same applies to base R and lattice plots.

In the context of some graph export functions I am writing I was wondering if it would be possible to write a function that would take the output of the current graphics device (which can be accessed using recordPlot()), and scale all plot symbols and text as well as line widths by an arbitrary %, thereby allowing the graph to be exported at any size, whilst still maintaining the same look? I.e. a function to proportionally scale the whole graph to a larger or smaller size while keeping the look the same?

PS the same would be required to be able to scale the RStudio plot window on high DPI 4K screens and still make it legible (right now RStudio uses pixel doubling but this makes the plot window look really fuzzy, and also causes problems in combination with Cleartype on Windows, causing colour fringing).

解决方案

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)

这篇关于R:函数按比例缩放ggplot2,格或基R图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆