0

I have a long dataframe with the following structure:

FROM TO WEIGHT NAME_1 NAME_2 CATEGORY
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

The idea is to plot the network. For this, the following code works:

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'))

However, when I try to color the nodes based on their category (I have 7 different categories), I get an error:

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 = graph$category, 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'))

In particular, I receive the following error:

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

I already tried many of the solutions provided in other questions (for example, adding "factor(graph$category)" or using just "category"). What am I missing?

ccfarre
  • 49
  • 1
  • 1
  • 5

1 Answers1

0

It seems that the problem was that instead of using

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

It has to be

geom_node_point(aes(color = V(graph2)$category, size = 5))
ccfarre
  • 49
  • 1
  • 1
  • 5