【问题标题】:ggraph change geom_node_text color based off key wordsggraph 根据关键字更改 geom_node_text 颜色
【发布时间】:2019-10-05 19:16:17
【问题描述】:

阅读Tidytext Mining with R --https://www.tidytextmining.com/nasa.html -- 我有以下问题:

默认情况下,节点文本颜色为黑色,我已经能够全局调整颜色 但是是否可以根据关键字使用默认颜色黑色但其他颜色?

library(ggplot2)
library(igraph)
library(ggraph)

set.seed(1234)
title_word_pairs %>%
 filter(n >= 250) %>%
 graph_from_data_frame() %>%
 ggraph(layout = "fr") +
 geom_edge_link(aes(edge_alpha = n, edge_width = n)
 , edge_colour = "cyan4") +
 geom_node_point(size = 5) +
 geom_node_text(aes(label = name), repel = TRUE
 , point.padding = unit(0.2, "lines"), colour="red") +
 theme_void()

在上图中,“land”和“data”为红色,所有其他文本为黑色。

【问题讨论】:

  • 请包含dput(title_word_pairs) 的输出,以便我们有一个可重现的示例。

标签: r ggplot2 igraph ggraph


【解决方案1】:

在没有可重现示例的情况下,我浏览了 link 并制作了一个小数据集来说明我的解决方案。

library(dplyr)
library(widyr)
library(ggplot2)
library(igraph)
library(ggraph)

数据

title_word_pairs1 <- structure(list(item1 = c("phase", "ges", "phase", "1", "phase", 
                                              "phase", "ges", "disc", "phase", "phase"), 
                                    item2 = c("ii", "disc", "system", "version", "space",
                                              "based", "degree", "degree", "low", "power"), 
                                    n = c(2498, 1201, 948, 678, 637, 601, 
                                          582, 582, 480, 441)), 
                                row.names = c(NA, -10L), 
                                class = c("tbl_df", "tbl", data.frame"))

使用ggplot 数据创建颜色列表:

在这里,我创建了没有节点、文本等的igraph,然后使用它的数据来制作所需颜色的列表。

set.seed(1)
g <- title_word_pairs1 %>%
  filter(nnn >= 250) %>%
  graph_from_data_frame() %>%
  ggraph(layout = "fr")

mcolor <- g$data %>% mutate(mcolor = if_else(name %in% c("low", "space"), 
                                     "blue", "black")) %>% select(mcolor)

g +
  geom_edge_link(aes(edge_alpha = n, edge_width = n)
                 , edge_colour = "cyan4") +
  geom_node_point(size = 5) +
  geom_node_text(aes(label = name), repel = TRUE
                 , point.padding = unit(0.2, "lines"), colour=mcolor$mcolor) +
  theme_void() + theme(legend.position="none")

reprex package (v0.2.1) 于 2019-05-19 创建

ggplot_build 对象颜色操作为所需颜色:

我基本上做的是制作情节,然后操纵ggplot 对象以获得所需的颜色。

set.seed(1)
g <- title_word_pairs1 %>%
  filter(n >= 250) %>%
  graph_from_data_frame() %>%
  ggraph(layout = "fr") +
  geom_edge_link(aes(edge_alpha = n, edge_width = n)
                 , edge_colour = "cyan4") +
  geom_node_point(size = 5) +
  geom_node_text(aes(label = name), repel = TRUE
                 , point.padding = unit(0.2, "lines"), colour="red") +
  theme_void() + theme(legend.position="none")

g

gg <- ggplot_build(g)

gg$data[[3]] <- gg$data[[3]] %>%
                             mutate(colour = if_else(label %in% c("low", "space"), 
                                                    "blue", "black"))

gt <- ggplot_gtable(gg)

plot(gt)

reprex package (v0.2.1) 于 2019 年 5 月 18 日创建

【讨论】:

  • 感谢您的帮助,符合我的要求,我已经在使用代码了。顺便说一句,有没有办法去除输出中的灰色背景?我试过使用 ggplot 的 panel.background = element_blank() 但没有运气。再次感谢您的帮助!
  • 忽略背景问题,一切就绪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
  • 2012-08-03
  • 2023-01-12
  • 2016-06-02
  • 2019-06-20
相关资源
最近更新 更多