【问题标题】:How to manually customize edge color and arrows in ggraph?如何在 ggraph 中手动自定义边缘颜色和箭头?
【发布时间】:2020-12-11 16:06:11
【问题描述】:

我正在尝试在自定义一些样式的同时可视化网络。到目前为止,我还没有成功完成任务。

要求:

  • 只有权重为 2 的边应该有一个指向链接方向的箭头。
  • 一条边的颜色应与其离开的节点颜色相同。

代码:

library(ggraph)
library(igraph)
library(graphlayouts)
library(tibble)
library(dplyr)

nodes <- data.frame(name = c('a','b','c','d','e')) 

links <- data.frame(
  from = c('a', 'b', 'd', 'a', 'd', 'a', 'c'),
  to = c('b', 'd', 'e', 'c', 'a', 'e', 'e'),
  weight = c(1, 1, 2, 2, 2, 1, 2),
  linetype = c('dashed', 'dashed', 'solid', 'solid', 'solid', 'dashed', 'solid')
)


tbl_graph_net <- as.igraph(tidygraph::tbl_graph(nodes = nodes, edges = links))

layout <- create_layout(tbl_graph_net, 
                        layout = 'igraph', 
                        algorithm = "nicely")

colfunc <- colorRampPalette(c("blue", "red"))
cols <- colfunc(5)

ggraph(tbl_graph_net, layout = "stress") +
  # Edges
  geom_edge_fan(aes(colour = from, 
                    edge_width = weight, 
                    linetype = linetype)) +
  scale_edge_linetype_manual(limits = as.factor(links$linetype), 
                             values = links$linetype) +
  scale_edge_colour_gradient(low = "blue", high = "red") +
  # Nodes
  geom_node_point(colour = cols, size = 10) +
  # Labels
  geom_node_text(aes(label = name), 
                 size = 7, 
                 colour = "#FFFFFF") +
  # Theme
  theme_graph() +
  theme(legend.position = "none")

我知道scale_edge_color_manual 的存在,但不知道如何正确使用它。同样,我不知道我是否使用scale_color_manual 正确缩放了节点的颜色。

匹配边缘和节点颜色绝对是可能的,但我不知道如何仅在某些边缘上绘制箭头。如果ggraph 不允许,我很乐意接受igraph 或类似的其他解决方案。

编辑:

我设法明显匹配边缘和节点颜色。代码已更新。只有箭头不见了

【问题讨论】:

    标签: r ggplot2 igraph graph-visualization ggraph


    【解决方案1】:

    您可以在 ggraph 中使用 geom_edge_link2() 变体使边缘与它们来自的节点颜色相同。

    关于箭头,我最近问了一个类似的问题。使用 base/igraph here

    为我提供了一个解决方案

    【讨论】:

    • 我正在尝试 geom_edge_link2 和 geom_edge_fan2 但无法使其工作。我需要通过特定的审美吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2022-12-17
    • 2019-06-19
    • 1970-01-01
    相关资源
    最近更新 更多