在 ggimage geom_image() 绘图重叠时添加抖动

问题描述 投票:0回答:1

让我们以显示图像而不是 ggplot 上的点为例。

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                image = sample(c("https://www.r-project.org/logo/Rlogo.png",
                                 "https://jeroenooms.github.io/images/frink.png"),
                               size=10, replace = TRUE)
)
# plot2
ggplot(d, aes(x, y)) + geom_image(aes(image=image), size=.08)

我需要修改代码,以便在图像重叠(或基于 x,y 彼此更接近)时添加一些距离/抖动。如果没有太复杂的解决方法,这可能吗?

r ggplot2 ggimage
1个回答
0
投票

尝试

position = position_jitter()

(如果需要,您可以在

seed
中设置
position_jitter
以提高重现性。)

library(ggplot)
library(ggimage)

set.seed(2)
d <- data.frame(
  x = rnorm(10),
  y = rnorm(10),
  image = sample(
    c(
      "https://www.r-project.org/logo/Rlogo.png",
      "https://jeroenooms.github.io/images/frink.png"
    ),
    size = 10,
    replace = TRUE
  )
)

ggplot(d, aes(x, y)) +
  geom_image(aes(image = image), size = .08, position = position_jitter(width = 0.2))

© www.soinside.com 2019 - 2024. All rights reserved.