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


i want arrange image along side plots. example looks this:

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) 

which looks like: enter image description here

i want car image height match height of 3 plots.

additionally in actual data plots have differing x axis lengths there way plot them x axes scaled relative each other?

thanks in advance.

try this:

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 , below 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

cropping white space above , below car:

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

stretching cropped image:

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


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -