2

Reading Tidytext Mining with R --https://www.tidytextmining.com/nasa.html -- I have the following question:

By default the node text color is black and I've been able to adjust the color globally but is it possible to have the default color black but other color(s) based off key words?

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()

enter image description here

In the above image "land" and "data" would be red and all other text would be black.

M--
  • 25,431
  • 8
  • 61
  • 93
Atwp67
  • 307
  • 5
  • 21
  • Please include output of `dput(title_word_pairs)` so we have a reproducible example. – M-- May 18 '19 at 23:44

1 Answers1

2

Without a reproducible example I went through the link and made a small dataset to illustrate my solution.

Libraries

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

Data

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"))

Using ggplot data to create a list of colors:

Here, I created the igraph without nodes, text, etc. and then used its data to make a list of desired colors.

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")

Created on 2019-05-19 by the reprex package (v0.2.1)

Manipulating ggplot_build object colors to desired colors:

What I basically do is making the plot and then manipulating the ggplot object to get the desired colors.

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)

Created on 2019-05-18 by the reprex package (v0.2.1)

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    thanks for the assist, lines up to my request and I'm already putting the code to use. As an aside, is there a way to remove the grey background on the output? I've tried using ggplot's panel.background = element_blank() but no luck. Again, thanks for the help! – Atwp67 May 19 '19 at 14:55
  • 1
    disregard on the background question, got it all set up. – Atwp67 May 19 '19 at 15:04