geom_node_image() - ggraph 中节点的图像

geom_node_image() - Images for nodes in ggraph

我正在尝试在 ggraph 网络中使用图像(例如国旗)。我一直在寻找使用 ggimage 的 geom_image,但是我认为该函数需要进行调整才能与 ggraph 一起使用,我们没有指定 x 和 y 坐标。

library(tidygraph)
library(ggraph)
library(ggimage)

r <- create_notable('bull') %>%
  mutate(class = sample(letters[1:3], n(), replace = TRUE),
         image = "https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png")

ggraph(r, 'stress') + 
  geom_node_point() +
  geom_edge_link()


r %>% 
  as_tibble() %>%
  ggplot(aes(x = runif(nrow(.)), y = runif(nrow(.)))) +
  geom_image(aes(image = image))

# I would like this to work:
ggraph(r, 'stress') + 
  geom_node_image(aes(image = image)) +
  geom_edge_link()

确实ggraph不需要您指定X和Y坐标,但这是一种方便。变量名称为 xy。你可以明确:

ggraph(r, 'stress') + 
  geom_node_point(aes(x = x, y = y))

并且这些变量可用于所有其他 ggplot 相关函数,包括 ggimage::geom_image()

ggraph(r, 'stress') + 
  geom_edge_link() + 
  geom_image(aes(x = x, y = y, image = image))