R - 在网格中排列时如何更改图像的高度

R - How to change the height of images when arranging in grids

我想在一些情节旁边安排一张图片。我的示例如下所示:

library(grid)
library(gridExtra)
library(ggplot2)
library(RCurl)
library(png)

img0 <- readPNG(getURLContent('http://carpng.com/wp-content/uploads/thumb/red-cartoon-car-8056-0.png'))
grob0 <- rasterGrob(img0)

p <- ggplot(mpg, aes(displ, hwy))+ geom_point()+ geom_line()
p3 <- grid.arrange(p,p,p)

grid.arrange(grob0, p3, ncol=2)

看起来像:

我想让汽车图像高度与三个地块的高度相匹配。

此外,在我的实际数据中,绘图具有不同的 x 轴长度,有没有办法绘制它们以便 x 轴相对于彼此缩放?

提前致谢。

试试这个:

library(grid)
library(gridExtra)
library(ggplot2)
library(RCurl)
library(png)

img0 <- readPNG(getURLContent('http://carpng.com/wp-content/uploads/thumb/red-cartoon-car-8056-0.png'))

# Set height of image.
# Image has white space above and below the car
grob0 <- rasterGrob(img0, height= unit(1,"npc"), just=c("center","center"))

p <- ggplot(mpg, aes(displ, hwy))+ geom_point()+ geom_line()
p3 <- grid.arrange(p,p,p)
grid.arrange(grob0, p3, ncol=2)

在汽车上方和下方裁剪白色space:

img0 <- img0[75:225,,]
grob0 <- rasterGrob(img0, height= unit(1,"npc"), just=c("center","center"))
grid.arrange(grob0, p3, ncol=2)

拉伸裁剪后的图像:

grob0 <- rasterGrob(img0, width=unit(1,"npc"), height=unit(1,"npc"),
         just=c("center","center"))
grid.arrange(grob0, p3, ncol=2)