问题描述
我正在学习使用 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 的答案都是不错的选择。如果没有,那没关系。