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)
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)
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)
stretching cropped image:
grob0 <- rastergrob(img0, width=unit(1,"npc"), height=unit(1,"npc"), just=c("center","center")) grid.arrange(grob0, p3, ncol=2)
Comments
Post a Comment