ggraph 不能 select 列用于 geom_node 或 geom_node_text aes 变量

ggraph can't select columns for geom_node or geom_node_text aes variables

当我尝试为 geom_node_point 或 geom_node_text 使用变量时,无论我做什么,我都会不断出错。这是我最初尝试的方法。

library(igraph)
library(ggraph)
library(tidyverse)
graph3 <- graph_from_data_frame(test, directed = F)
ggraph(graph2, layout = "kk") + 
  geom_edge_link(aes(colour = match, alpha = match)) +  geom_node_point(aes(colour = qcluster, alpha =match)) +geom_node_text(repel=TRUE)#  theme(legend.position = "none")

但这给了我以下错误

Error in FUN(X[[i]], ...) : object 'qcluster' not found

那么如果我尝试使用更精确的指令来显示要使用的列,我会遇到其他问题

ggraph(graph3, layout = "kk") + 
geom_edge_link(aes(colour = match, alpha = match)) +  geom_node_point(aes(colour = test$qcluster, alpha =match)) +geom_node_text(repel=TRUE)#  theme(legend.position = "none")

returns错误

Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error: Aesthetics must be valid data columns. Problematic aesthetic(s): alpha = match. Did you mistype the name of a data column or forget to add after_stat()?

所以我的问题是,如何让列作为 Aes 变量工作,它们似乎在 geom_edge_link 中工作得很好,所以我真的很困惑究竟出了什么问题。特别是因为手动添加字符串或数字等 aes 变量似乎工作得很好。 The test dataset, it's a very small subsection of my data so testing goes easier

query subject qcluster scluster match bit_sum
16T1_lib200772_4_13 YP_009910784.1 cr165 cr209 False 57.4
16T1_lib200772_4_17 YP_009910789.1 cr177 cr241 False 131.0
16T1_lib200772_4_17 YP_009910790.1 cr177 cr230 False 57.4
16T1_lib200772_4_23 YP_009910794.1 cr7 cr7 True 69.3
16T1_lib200772_4_82 YP_009910759.1 cr1 cr1 True 92.8
16T1_lib200772_4_83 YP_009910760.1 cr6 cr6 True 79.3

我对 ggraph 也很陌生 - 但是,我发现,当您使用 graph_from_data_frame(或使用 tidygraphas_tbl_graph)创建图形时,仅一个属性是为顶点创建的,所有其他属性最终都作为无法被 geom_node_... 访问的边属性。所以,我的解决方法是手动添加顶点属性。您收到的第二条错误消息是由于 match() 是 R 中的一个函数。下面是一个示例,说明它是如何工作的。在这种情况下,我非常懒惰,相当随意地添加了顶点属性——你需要为你的顶点选择正确的属性:

test = data.frame(
        query = c("16T1_lib200772_4_13", "16T1_lib200772_4_17",
                "16T1_lib200772_4_17",  "16T1_lib200772_4_23",
                "16T1_lib200772_4_82",  "16T1_lib200772_4_83"),
        subject = c("YP_009910784.1",   "YP_009910789.1",
                "YP_009910790.1",   "YP_009910794.1",
                "YP_009910759.1",   "YP_009910760.1"),
        qcluster = c("cr165",   "cr177",
                "cr177",    "cr7",
                "cr1",  "cr6"),
        scluster = c("cr209",   "cr241",
                "cr230",    "cr7",
                "cr1",  "cr6"),
        my_match = c(FALSE, FALSE,
                FALSE,  TRUE,
                TRUE,   TRUE),
        bitsum = c(57.4,    131.0,
                57.4,   69.3,
                92.8,   79.3))

library(igraph)
library(ggraph)
library(tidyverse)
#library(tidygraph)
graph3 <- graph_from_data_frame(test, directed = F)

#########################################################
#### quick and dirty addition of a vertex attribute: ####
graph3 <- set_vertex_attr(graph3, "qcluster", value = c("cr165",
                "cr177", "cr177", "cr7",
                "cr1",  "cr6", "cr177", "cr177",
                "cr7",  "cr1", "cr6"))
#########################################################

#graph3 <- as_tbl_graph(graph3) 

ggraph(graph3, layout = "kk") + 
  geom_edge_link(aes(colour = my_match)) +
  geom_node_point(aes(colour = qcluster)) +
  geom_node_text(repel=TRUE)#  theme(legend.position = "none")

基于 Wolfgang 的回答,我找到了一种无需手动应用即可添加顶点信息的方法。 graph3 <- graph_from_data_frame(test, directed = F, vertices = unique(FileNames)) 其中 FileNames 是我的图表中包含元数据的文件。如果您将此与 Wolfgang 的回答结合起来,您将得到您正在寻找的图表