[英]How to dynamically assign border color using geom_node_circle?
我正在尝试使用geom_node_circle
更改ggraph
中节点的边界。 我想根据节点名称与我为名称提供的 colors 之间的映射来设置边界 colors。 我知道改变边框的参数是colour
。
制作图表例如:
df <- data_frame(group = c("animals","animals","pets","pets","pets","wild animals","wild animals"),
subgroup = c("pets","wild animals","rabbit","dog","cat","polar bear","panda bear"))
df <- as.data.frame(table(df))
df <- filter(df, Freq > 0)
vertices <- df %>%
dplyr::distinct(subgroup, Freq) %>%
dplyr::add_row(subgroup = "animals", Freq = 0)
graph <- graph_from_data_frame(df, vertices = vertices)
我知道如何将所有边框更改为一种新颜色(红色):
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill =depth),colour = "red") +
coord_fixed() +
geom_node_label(aes(label = name), repel = TRUE, size = 5)
但是,我不知道如何根据graph
中name
与我的 colors 之间的映射来分配边界。
尝试A
制作 map 并应用于ggraph
:
my_colors <- c("black","black","red","black","black","black","black","black")
names(my_colors) <- c("animals","pets","wild animals","rabbit","dog","cat","polar bear","panda bear")
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill =depth),colour = my_colors) +
coord_fixed() +
geom_node_label(aes(label = name), repel = TRUE, size = 5)
退货
Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (2888): colour
Run `rlang::last_error()` to see where the error occurred.
尝试 B
将颜色列添加到原始df
并应用于ggraph
:
df$my_colors <- cbind(df, ifelse(df$subgroup=="wild animals", "red", "black")
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill =depth),colour = df$my_colors) +
coord_fixed() +
geom_node_label(aes(label = name), repel = TRUE, size = 5)
返回与以前相同的错误。
当我查看图表时,我可以看到my_colors
不存在:
create_layout(graph, 'circlepack', weight = Freq)
Non-leaf weights ignored
x y r circular leaf depth name Freq .ggraph.orig_index .ggraph.index
1 0.0000000 0.0000000 2.3440385 FALSE FALSE 0 animals 0 8 1
2 0.7244867 -0.8650770 1.2156595 FALSE FALSE 1 pets 1 4 2
3 -0.7805260 0.9319910 1.1283791 FALSE FALSE 1 wild animals 1 7 3
4 0.2584208 -0.4098895 0.5641896 FALSE TRUE 2 cat 1 1 4
5 1.3517237 -0.6890459 0.5641896 FALSE TRUE 2 dog 1 2 5
6 -0.5085709 0.4376730 0.5641896 FALSE TRUE 2 panda bear 1 3 6
7 -1.0524811 1.4263089 0.5641896 FALSE TRUE 2 polar bear 1 5 7
8 0.5633157 -1.4962957 0.5641896 FALSE TRUE 2 rabbit 1 6 8
尝试 C
尝试将my_color
添加到vertices
:
vertices$my_color <- ifelse(vertices$subgroup=="wild animals", "red", "black")
graph <- graph_from_data_frame(df, vertices = vertices)
# check if my_color is there
create_layout(graph, 'circlepack', weight = Freq)
我可以看到my_color
现在在graph
:
Non-leaf weights ignored
x y r circular leaf depth name Freq my_color .ggraph.orig_index .ggraph.index
1 0.00000000 0.0000000 2.3440387 FALSE FALSE 0 animals 0 black 8 1
2 0.42321874 1.0460045 1.2156596 FALSE FALSE 1 pets 1 black 4 2
3 -0.45595483 -1.1269132 1.1283791 FALSE FALSE 1 wild animals 1 red 7 3
4 1.05259805 1.2142151 0.5641896 FALSE TRUE 2 cat 1 black 1 4
5 0.25420381 0.4168406 0.5641896 FALSE TRUE 2 dog 1 black 2 5
6 -0.05740794 -0.7275760 0.5641896 FALSE TRUE 2 panda bear 1 black 3 6
7 -0.85450171 -1.5262505 0.5641896 FALSE TRUE 2 polar bear 1 black 5 7
8 -0.03714565 1.5069576 0.5641896 FALSE TRUE 2 rabbit 1 black 6 8
但如果我跑
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill =depth),colour = my_color) +
coord_fixed() +
geom_node_label(aes(label = name), repel = TRUE, size = 5)
我明白了
Error in layer(data = data, mapping = mapping, stat = StatNodeCircle, :
object 'my_color' not found
所以,然后我发现我只是不知道如何从graph
访问属性,但我想通了,并使用colour = V(graph)$my_color
将其分配给边框颜色。 这仍然会导致相同的错误,表明数据的长度是 2888。我假设这个错误被抛出,因为我的 colors 列表小于那个。
想法
我是使用ggraph
的新手,我认为我缺乏一些基本知识来解决这个问题....
解决方案
从@DanAdams 原始答案修改:
df <- data_frame(group = c("animals","animals","pets","pets","pets","wild animals","wild animals"),
subgroup = c("pets","wild animals","rabbit","dog","cat","polar bear","panda bear"))
df <- as.data.frame(table(df))
df <- filter(df, Freq > 0)
vertices <- df %>%
dplyr::distinct(subgroup, Freq) %>%
dplyr::add_row(subgroup = "animals", Freq = 0)
vertices$my_color <- ifelse(vertices$subgroup=="wild animals", "red", "black")
graph <- graph_from_data_frame(df, vertices = vertices)
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill =depth,colour = my_color)) +
coord_fixed() +
geom_node_label(aes(label = name), repel = TRUE, size = 5)
看来您只需要在aes()
中添加color = name
即可。
我还为标签着色以使其更容易理解。
library(tidyverse)
library(igraph)
library(ggraph)
df <- data_frame(group = c("animals","animals","pets","pets","pets","wild animals","wild animals"),
subgroup = c("pets","wild animals","rabbit","dog","cat","polar bear","panda bear"))
my_colors <- c("black","black","red","black","black","black","black","black")
names(my_colors) <- c("animals","pets","wild animals","rabbit","dog","cat","polar bear","panda bear")
df <- as.data.frame(table(df))
df <- filter(df, Freq > 0)
vertices <- df %>%
dplyr::distinct(subgroup, Freq) %>%
dplyr::add_row(subgroup = "animals", Freq = 0)
graph <- graph_from_data_frame(df, vertices = vertices)
ggraph(graph, layout = "circlepack", weight = Freq) +
geom_node_circle(aes(fill = depth, color = name), size = 1) +
coord_fixed() +
geom_node_label(aes(label = name, color = name), repel = TRUE, size = 5, show.legend = F) +
scale_color_manual(values = my_colors)
由代表 package (v2.0.1) 于 2022 年 9 月 22 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.