简体   繁体   English

R:按比例缩放ggplot2,格子或基本R图的函数

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

In RI 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. 在RI中总是发现令人讨厌的是基础R,晶格和ggplot2绘制所有工作的绝对点大小的文本和绘图符号的大小。

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. 这同样适用于基础R和晶格图。

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? 在我编写的一些图形导出函数的上下文中,我想知道是否可以编写一个函数来获取当前图形设备的输出(可以使用recordPlot()访问),并缩放所有绘图符号和文本以及任意%的线宽,从而允许图形以任何大小导出,同时仍然保持相同的外观? Ie 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). PS 同样需要能够在高DPI 4K屏幕上缩放RStudio绘图窗口并且仍然使其清晰可读(现在RStudio使用像素倍增但这使得绘图窗口看起来非常模糊,并且还导致与Cleartype结合的问题在Windows上,导致颜色边缘)。

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. 由于某种原因,点大小被编码为fontsize,因此将其转换为可自动缩放的单位似乎并不容易。 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)

在此输入图像描述

This seems to be closer to your end goal 这似乎更接近你的最终目标

library(ggplot2)
p <- qplot(Sepal.Length, Petal.Length, data = iris, 
           geom=c("point","line"),
           color = Species, size = Petal.Width, alpha = I(0.7))
library(grid)
print(p, vp=viewport(gp=gpar(cex=2, lex=2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM