角度为何会使geom_image点漂移

时间:2018-06-23 21:36:28

标签: r ggplot2

为什么“ angle”参数使ggplot2中geom_image中的点偏离所需位置?

演示数据:

d <- data.frame(x = 1:N,
            y = 1:N,
            image = rep("https://www.r-project.org/logo/Rlogo.png", N),
            angle = seq(0, 45, length.out=N)
            )

演示:

library("ggplot2")
library("ggimage")

p <- ggplot(d, aes(x, y)) 
for (i in 1:dim(d)[1]){
  di <- d[i, ]
  p = p + 
    geom_image(data = di, aes(image=image), angle = di$angle[1]) +
    geom_point(data=di)
}
p 

enter image description here

1 个答案:

答案 0 :(得分:2)

ggimage在旋转的视口中的(x,y)处绘制每个图像;相反,它应该在放置在(x,y)的旋转视口内绘制。

library("ggplot2")
library("grid")

grid.newpage()
grid.points(x = 0.5, y = 0.8, default.units = "npc", pch="+",gp=gpar(cex=5))
grid.raster(png::readPNG(system.file("img", "Rlogo.png", package="png")), 
            x = 0.5, y = 0.8, vp = viewport(angle=45), height=unit(1,"cm"))
grid.raster(png::readPNG(system.file("img", "Rlogo.png", package="png")), 
            vp = viewport(x = 0.5, y = 0.8, angle=45), height=unit(1,"cm"))