首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:按比例缩放ggplot2、晶格或基础R图的函数

R:按比例缩放ggplot2、晶格或基础R图的函数
EN

Stack Overflow用户
提问于 2015-07-13 18:34:59
回答 2查看 936关注 0票数 11

在R中,我总是觉得基础R、格子和ggplot2图都与文本和绘图符号大小的绝对点大小一起工作,这很烦人。

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

代码语言:javascript
复制
windows(width=5,height=5);qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))

代码语言:javascript
复制
windows(width=10,height=10);qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))

文本和绘图符号的相对比例相对于其他符号发生了变化。这同样适用于基数R和点阵图。

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

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

EN

回答 2

Stack Overflow用户

发布于 2015-09-05 16:38:11

由于某些原因,磅大小被编码为fontsize,因此将其转换为自动缩放的单位似乎并不容易。如果您知道要应用哪种缩放方式,那么以下内容可能会有所帮助

代码语言:javascript
复制
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)

票数 5
EN

Stack Overflow用户

发布于 2015-09-05 19:11:49

这似乎更接近你的最终目标。

代码语言:javascript
复制
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)))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31381066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档