如何缩放子图的全部内容?

我找不到缩放整个子图(文本,行等)的方法。我尝试过的所有内容(带有coord_cartesian,ggdraw和draw_plot的annotation_custom)都会缩放边框,但不会缩放内容。

我想避免渲染图,然后再使用图像。任何帮助表示赞赏。

示例代码:

mydata <- data.frame(x = c(1,2,3),y= c('a','b','c'))
g <- ggplot(data = mydata) + geom_point(aes(x,y)) 
ggdraw(g + draw_plot(g,x=1,y= 1,width=1,height=1,scale=0.2))

结果:

A plot embedded in itself as subplot,with only the bounding box scaling

dubujiewu 回答:如何缩放子图的全部内容?

我不太确定您要问什么。如果这是关于如何在(gg)图中包含和缩放(gg)图 ,这是一个最小的可重现示例

gg1 <- ggplot(mtcars,aes(mpg,wt)) +
    geom_point() +
    geom_smooth(method = "lm")
gg2 <- ggplot(mtcars,hp)) +
    geom_point()

gg1 + annotation_custom(
    ggplotGrob(gg2),xmin = 25,xmax = 34,ymin = 3.5,ymax = 5.5)

enter image description here

子图的尺寸由坐标(xmin,ymin)(xmax,ymax)确定。


更新

为回应您的评论,也许这就是您所追求的?

library(grid)
library(png)
fn <- tempfile("plot",fileext = "png")
ggsave(filename = fn,plot = gg2,device = "png")
img <- readPNG(fn)
gg1 + annotation_custom(
    rasterGrob(img,interpolate = T),xmax = 28,ymax = 5)

这个想法是将子图保存在一个临时的PNG文件中。然后,我们可以使用png :: readPNG with grid :: rasterGrob`在主图中显示PNG作为自定义注释。

enter image description here

本文链接:https://www.f2er.com/3147551.html

大家都在问