微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 ggraph 更改 geom_node_point() 中的颜色

如何解决从 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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。