gpt4 book ai didi

R:按比例缩放 ggplot2、点阵或基本 R 图的函数

转载 作者:行者123 更新时间:2023-12-02 03:21:29 31 4
gpt4 key购买 nike

在 R 中,我总是觉得很烦人的是,基本 R、lattice 和 ggplot2 绘图都使用文本和绘图符号大小的绝对点大小。

这意味着,如果您增加绘图窗口的大小以获得填充页面的图表

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

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

文本和绘图符号的相对比例相对于其余部分发生变化。这同样适用于基本 R 和晶格图。

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

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).

最佳答案

出于某种原因,点大小被编码为字体大小,因此将其转换为自动缩放的单位似乎并不容易。如果您知道要应用什么缩放比例,那么以下内容可能会有所帮助

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)

enter image description here

关于R:按比例缩放 ggplot2、点阵或基本 R 图的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31381066/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com