r语言 - 将图像插入到日志图表 ggplot2



是否可以在R(ggplot2(中使用对数y轴将图像添加到图表中。我的意思是:

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

如果是 - 如何?

例如,它不起作用:

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
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"))

试试这个,

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")))

在此处输入图像描述

最新更新