[英]R ggraph facets
我很難弄清楚如何正確地刻面圖。
請參見以下示例。
library(tidyverse)
library(tidygraph)
library(ggraph)
myedges <- data.frame(from=rep(letters[1:5],2),
to=c(rep(LETTERS[12:13],2),
rep(LETTERS[14],3),
LETTERS[15:16],
LETTERS[17]),
n_user=sample(1:20, 10, replace=TRUE),
f=rep(c("face_A", "face_B"),5)
)
mynodes <- myedges %>%
pivot_longer(cols = c(from, to),
names_to = "od",
values_to = "node_name") %>%
group_by(node_name) %>%
summarise(n_user=sum(n_user))
g<-tbl_graph(nodes = mynodes, edges= myedges)
# Arc diagram with edge faceting, it seems ok but the nodes
ggraph(g, layout = 'linear') +
geom_edge_arc(aes(edge_width=n_user, label=n_user), label_pos=0.1, edge_alpha=0.2)+
geom_node_point(aes(size=n_user), alpha=0.5, show.legend = FALSE)+
geom_node_text(aes(label = node_name), size=3, nudge_y = - 0.5)+
facet_edges(facets = vars(f), scales = "free")
該圖幾乎沒問題,除了我希望每個面板只有通過邊有效連接的節點:即沒有“孤兒”節點,如面板“face_A”中的節點“O”或節點“P”在面板“face_B”中。
鑒於此,我希望facet_nodes
將成為我的潛在解決方案,方法是僅繪制由變量映射的節點以及該面板中存在的節點的相應邊緣。
我希望實現我需要在節點中引入映射變量,但問題來了,因為我無法構思出正確的方法來做到這一點。
預期結果是每個面板節點由邊連接的圖形
有什么建議嗎?
我認為這里的問題是您正在嘗試 plot 使用構面的兩個子圖,但是節點在構面變量中並不互斥。 這意味着您不能使用facet_node
。 由於您是按邊緣屬性分面的,因此您需要使用facet_edge
,但不幸的facet_edge
繪制了所有節點,沒有刪除孤立節點的選項。
解決這個問題的明顯方法(盡管有點費力)是 plot 兩個不同的子圖並將它們patchwork
起來:
library(patchwork)
gA <- g %>%
activate(edges) %>%
filter(f == "face_A") %>%
activate(nodes) %>%
filter(!node_is_isolated())
gB <- g %>%
activate(edges) %>%
filter(f == "face_B") %>%
activate(nodes) %>%
filter(!node_is_isolated())
p1 <- ggraph(gA, layout = 'linear') +
geom_edge_arc(aes(edge_width = n_user, label = n_user),
label_pos = 0.1, edge_alpha = 0.2) +
geom_node_point(aes(size=n_user), alpha = 0.5, show.legend = FALSE) +
geom_node_text(aes(label = node_name), size = 3, nudge_y = - 0.5) +
facet_grid(.~f) +
theme(legend.position = "none")
p2 <- ggraph(gB, layout = 'linear') +
geom_edge_arc(aes(edge_width = n_user, label = n_user),
label_pos = 0.1, edge_alpha = 0.2) +
geom_node_point(aes(size = n_user), alpha = 0.5, show.legend = FALSE) +
geom_node_text(aes(label = node_name), size = 3, nudge_y = - 0.5) +
facet_grid(.~f)
p1 + p2
通過進一步考慮上述方法,我得出了這種解決方案,但在某些角落仍然需要四舍五入; 部分代碼比較粗糙,但整體解決方案值得指出我想進一步的方向 go
我在這里發布完整的代碼
library(tidyverse)
library(tidygraph)
library(ggraph)
myedges <- data.frame(from=rep(letters[1:5],2),
to=c(rep(LETTERS[12:13],2),
rep(LETTERS[14],3),
LETTERS[15:16],
LETTERS[17]),
n_user=sample(1:20, 10, replace=TRUE),
f=rep(c("face_A", "face_B"),5)
)
mynodes <- myedges %>%
pivot_longer(cols = c(from, to),
names_to = "od",
values_to = "node_name") %>%
group_by(node_name) %>%
summarise(n_user=sum(n_user))
g<-tbl_graph(nodes = mynodes, edges= myedges)
graph_list <- list()
for(i in unique(myedges$f)) {
gr <- g %>%
activate(edges) %>%
filter(f == i) %>%
activate(nodes) %>%
filter(!node_is_isolated())
graph_list[[i]] <- gr
}
plot_list <- list()
for(i in seq_along(graph_list)){
pl<-ggraph(graph_list[[i]], layout = 'linear') +
geom_edge_arc(aes(edge_width = n_user, label = n_user),
label_pos = 0.1, edge_alpha = 0.2) +
geom_node_point(aes(size=n_user), alpha = 0.5, show.legend = FALSE) +
geom_node_text(aes(label = node_name), size = 3, nudge_y = - 0.5) +
facet_grid(.~f) +
theme(legend.position = "none")
plot_list[[i]] <- pl
}
library(ggpubr)
ggarrange(plotlist = plot_list,
ncol = max(seq_along(graph_list))
)
謝謝你的提示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.