从 ggraph 更改 geom_node_point() 中的颜色

问题描述

我有一个具有以下结构的长数据框:

来自 重量 NAME_1 NAME_2 类别
1 2 0.1 name_1 name_2 category_a
2 3 0.3 name_2 name_3 category_b
... ... ... ... ... ...
195 33 0.2 name_195 name_33 category_a

这个想法是绘制网络。为此,以下代码有效:

library(ggraph)
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
ggraph(graph,layout = "nicely") +
  geom_edge_link(aes(alpha = range01(weight),width = range01(weight)),edge_colour = "grey") +
  scale_edge_width(range = c(0.1,3))+
  geom_node_point(aes(color = "red",size = 5)) +
  ggtitle("Text Network") +
  labs(tag = "figure 2") +
  theme(panel.background = element_rect(fill = "white"),legend.position = "none",plot.title=element_text( hjust=0.5,vjust=0.5,face='bold'))

但是,当我尝试根据节点的类别(我有 7 个不同的类别)为节点着色时,出现错误

library(ggraph)
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
ggraph(graph,3))+
  geom_node_point(aes(color = graph$category,face='bold'))

特别是,我收到以下错误

Error: Aesthetics must be either length 1 or the same as the data (195): colour

我已经尝试了其他问题中提供的许多解决方案(例如,添加“factor(graph$category)”或仅使用“category”)。我错过了什么?

解决方法

似乎问题在于,而不是使用

geom_node_point(aes(color = graph$category,size = 5))

必须的

geom_node_point(aes(color = V(graph2)$category,size = 5))