没有可重复的示例,我通过链接并制作了一个小数据集来说明我的解决方案。
图书馆
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 包(v0.2.1)于 2019 年 5 月 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 包(v0.2.1)于 2019 年 5 月 18 日创建