繁体   English   中英

R ggraph / igraph geom_edge_link2 为因子变量定义自定义颜色

[英]R ggraph / igraph geom_edge_link2 define custom color for factor variable

我正在尝试更改边缘的颜色。 目前图表显示如下

图形

虽然根据图形属性“Polarität”(极性)对边缘进行着色,但我想将其从默认的 ggplot 红色和蓝色配色方案中切换。 我对如何做到这一点感到困惑。

提前致谢

ggraph <- ggraph(cog_tidy, layout = 'nicely',) + 
theme_graph()+
  scale_colour_brewer(palette = "Dark2") +
  geom_edge_link2(
    aes(end_cap = circle(6, "pt"), edge_color = Polarität ,width=Gewichtung),
    arrow = arrow(
      angle = 10,
      length = unit(0.1, "inches"),
      ends = "last",
      type = "closed"
    )
  )+ 
  geom_node_point( aes(size = Gradzentralität  ,color = Kategorie), show.legend = TRUE) +
  geom_node_text(aes(label = label), repel=TRUE, position = "identity", size=2) +
  scale_edge_width(range = c(0.1, 1)) 

您可以通过scale_edge_color_xxx系列函数设置用于边缘的颜色。 请参阅https://ggraph.data-imaginist.com/reference/index.html#section-scales

?geom_edge_link2scale_edge_color_manual调整默认示例,您可以分配特定的颜色,如下所示:

library(ggraph, warn = FALSE)
#> Loading required package: ggplot2
library(tidygraph, warn = FALSE)

set.seed(1)

gr <- create_notable('bull') %>%
  mutate(class = sample(letters[1:3], n(), replace = TRUE)) %>%
  activate(edges) %>%
  mutate(class = sample(letters[1:3], n(), replace = TRUE))

ggraph(gr, 'stress') +
  geom_edge_link2(aes(edge_color = node.class)) +
  scale_edge_color_manual(values = c(a = "purple", b = "orange", c = "black"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM