【问题标题】:how to color the nodes and labels in ggraph如何为ggraph中的节点和标签着色
【发布时间】:2019-12-16 23:02:43
【问题描述】:

如何像这样在 ggraph 图中指定边缘的颜色

library(ggraph)
library(igraph)
df_school = highschool %>% mutate(yr=ifelse(year%%2==0,'purple','blue'))
graph = df_school %>% graph_from_data_frame()
V(graph)$node_label = names(V(graph))
ggraph(graph, layout = 'linear', circular = TRUE) + 
  geom_edge_arc(aes(colour = factor(year))) + 
  geom_node_label(aes(label=node_label), alpha=0.2) + 
  scale_edge_colour_identity() 

这里只有边缘是彩色的,但我不知道如何编码节点或标签,或者使标签背景半透明。想知道是否有人可以提供帮助。

【问题讨论】:

  • 它缺少您定义的代码的一部分 graph (并且您正在使用 dplyr 而没有提及它)。但是,您可以使用geom_node_label() 中的alpha 使标签背景透明。给节点着色,需要指定颜色和节点,看here

标签: r ggraph


【解决方案1】:

您分别控制节点和节点标签。前缀是geom_node_——有很多选项。以下代码生成此图。

ggraph(graph, layout = 'linear', circular = TRUE) + 
  geom_edge_arc(aes(colour = factor(year))) + 
  geom_node_point(size = 8)+ # add nodes
  geom_node_text(aes(label = name), # add node labels
                 colour = 'white')+
  coord_equal()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多