-2

Is it possible in R (ggplot2) to add an image to a chart with log y-axis. I mean:

ggplot(mtcars, aes(x=mpg, y=disp))+
geom_line()+
coord_trans(y="log2")

If Yes - How?

For example it doesn't work:

image <- readPNG('/myFolder/car.png')

ggplot(mtcars, aes(x=mpg, y=disp))+
geom_line()+
coord_trans(y="log2")+
annotation_raster(image, ymin = 100, ymax= 200, xmin = 15, xmax = 
20,interpolate = FALSE)

Error: annotation_raster only works with Cartesian coordinates
MariaMaria1
  • 25
  • 1
  • 6

2 Answers2

0
library(ggplot2)
library(magick)
library(grid)

image <- image_read('/myFolder/car.png')

ggplot(mtcars, aes(x=mpg, y=disp))+
geom_line()+
coord_trans(y="log2")

// add image
grid.raster(image, unit(0.95, "npc"), y = unit(0.95, "npc"))
MariaMaria1
  • 25
  • 1
  • 6
0

Try this,

library(egg)

image <- readPNG(system.file("img", "Rlogo.png", package="png"))

dummy <- data.frame(x=15,y=100, data=I(list(image)))
ggplot(mtcars, aes(x=mpg, y=disp))+
  geom_line()+
  coord_trans(y="log2")+
  geom_custom(data=dummy, aes(x,y,data=data), 
              grob_fun=rasterGrob,
              fun_params = list(height=unit(1,"cm")))

enter image description here