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

标签 r ggplot2 gridextra

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

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)

看起来像: enter image description here

我希望汽车图片的高度与三个地 block 的高度相匹配。

此外,在我的实际数据中,绘图具有不同的 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)

enter image description here

裁剪汽车上方和下方的空白区域:

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

enter image description here

拉伸(stretch)裁剪后的图像:

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

enter image description here

关于R - 在网格中排列时如何更改图像的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753792/

相关文章:

r - for循环中的多个图忽略par

r - 一个图中的两个 ggplots,靠得很近

r - 如何在R中创建分布矩阵

r - 优化每行数据表的调用函数

r - 库中出现错误(插入符号): there is no package called ‘caret’

从 ggplot 中删除 n 个图例

在多个数据框列中重新编码 NA

r - 如何从 ggplot2 中提取箱数和百分比?

r - 华夫饼包 : Colors mixed up with a Zero/0 in data

r - 获取 axis.text 和 axis.title 之间的边距值