R:如何使用分类变量来设置图像插入的限制

R: How to use categorical variables to set limit for image insertion

我知道如何使用 annotation_custom() 函数将 .png 图像插入 table,但是当 x 轴上的变量是分类变量时,我可以使用相同的函数吗?如果可以的话,我应该如何修改语法呢?是否有任何可能的方法将分类变量转换为定性变量? 我已经编辑了我的命令,但我仍然看不到图像。这是我的命令:

AmericanAirlines <- readPNG("C:\Users\Desktop\AU 19\Lab 5\American Airlines.png")
dim(AmericanAirlines)
grobAA <- rasterGrob(AmericanAirlines, interpolate=TRUE)
plot3 + annotation_custom(grobAA, xmin=1, xmax=2, ymin=1, ymax=2)

p.s.: 我觉得plot3没什么问题,能顺利拿到plot

感谢您的回答!

ggplot 在内部将分类值放在 x 轴上的位置 1、2、3 等处,因此您可以在确定放置图像的位置时使用这些值作为比例。例如,使用内置的 iris 数据框和 Rlogo 图像:

img = png::readPNG(system.file("img", "Rlogo.png", package="png"))
img = rasterGrob(img, interpolate=TRUE)

ggplot(iris, aes(Species, Petal.Width)) + 
  geom_point() + 
  annotation_custom(img, xmin=0.8, xmax=1.2, ymin=0.7, ymax=1.1) +
  annotation_custom(img, xmin=1.2, xmax=1.8, ymin=1.4, ymax=2) +
  annotation_custom(img, xmin=2.1, xmax=2.3, ymin=0.5, ymax=0.7)