将有向边属性映射到ggraph中的scale_edge_color_gradient aes

问题描述

我正在学习使用 tidygraph ggraph 绘制社交网络数据。到目前为止,我还是很喜欢的,但是我无法弄清楚如何使用颜色渐变来指示边缘方向,同时又将颜色(渐变基于的颜色)映射到edge属性

示例数据

require(tidygraph)
require(ggraph)

edges_df <- tibble(from = c("A","A","B","C","D"),to = c("C","D","A"),edgewidth = c(1,3,2,4,1),edgegradient = factor(c("red","blue","red","red"))) 
# I realize edge gradient Could be any attribute,not necessarily the names of the desired colors.

nodes_df <- tibble(node_id = c("A","D"))

my_graph <- tbl_graph(directed = TRUE,nodes = nodes_df,edges = edges_df)

ggraph(my_graph %>%
         activate(edges),layout = "linear",circular = TRUE) +
  geom_edge_arc(aes(width = edgewidth,color = stat(..index..))) +
  geom_node_label(aes(label = node_id),size = 10)

Here is the ggraph plot (since I'm new to SO,I can't embed the image).

我想要的:边A-> C,D-> A和B-> D为红色渐变,边A-> B,C-> B和C-> D为蓝色梯度。我觉得 geom_edge_arc 的色彩美学和 scale_edge_color _ *美学的色彩令人困惑。

我喜欢 ggraph 使用 ggplot 引擎和类似的语法,因此,只要有可能,使用 ggraph 的答案都是不错的选择。如果没有,那没关系。

解决方法

也许这就是您想要的。通过良好指定的颜色渐变可能有解决方案。但是,查看问题后,获得两个不同“颜色渐变”的简单解决方案是将testRuntimeOnly "org.junit.platform:junit-platform-commons:1.7.0" 映射到pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' 美观度上,同时将edgegradient映射到..index..上。要获得正确的颜色,可以使用alpha

color

enter image description here