gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:31:36 24 4
gpt4 key购买 nike

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

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/

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