ggraph icon indicating copy to clipboard operation
ggraph copied to clipboard

Issue changing color in geom_node_point()

Open ccfarre opened this issue 3 years ago • 1 comments

I have a long dataframe with the following structure:

FROM: 1 / 2 / ... / 195 TO: 2 / 3 / ... / 33 WEIGHT: 0.1 / 0.3 / ... / 0.2 NAME_1: name 1 / name_2 / ... / name_195 NAME_2: name 2 / name_3 / ... / name_33 CATEGORY: category_a / category_b / ... / category_a

(Rows in this example are actually columns)

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 avatar Jul 07 '21 18:07 ccfarre

Hard to say if there is something else going wrong, but assuming graph is an igraph object then what graph$category does is access a graph level attribute. The actual syntax is exactly the same as for edges. Here is an example wit randomly generated data

library(ggraph) 
library(igraph)

graph <- sample_gnp(195,0.07)
E(graph)$weight <- runif(ecount(graph))
V(graph)$category <- sample(c("cat1","cat2","cat3","cat4","cat5","cat6","cat7"),195,replace = TRUE)
  
range01 <- function(x){(x-min(x))/(max(x)-min(x))} 

ggraph(graph, layout = "nicely") + 
  geom_edge_link0(aes(alpha = range01(weight), width = range01(weight)), edge_colour = "grey") + 
  scale_edge_width(range = c(0.1, 3))+ 
  geom_node_point(aes(color = 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'))

Created on 2022-05-03 by the reprex package (v2.0.1)

schochastics avatar May 03 '22 19:05 schochastics