使用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”将为红色,而所有其他文本将为黑色。
答案 0 :(得分:1)
在没有可重复的示例的情况下,我经历了link,并制作了一个小的数据集来说明我的解决方案。
使用ggplot
数据创建颜色列表:
在这里,我创建了没有节点,文本等的igraph
,然后使用其数据列出了所需的颜色。
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"))
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
对象以获得所需的颜色。
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"))
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-05-18创建