我想通過調整列數(例如從一列九行到三列三行)來修改我的情節中的圖例(見下文)。我已經嘗試添加guides(alpha = guide_legend(ncol = 3))
以調整列數。但是,這不起作用(我認為“alpha”不是正確的論點,但我找不到合適的論點)。
示例代碼和繪圖:
library(dplyr) #required for tibbles
library(igraph) #required for plotting
library(ggraph) #required for plotting
library(Cairo) #required for anti-aliasing
nodes = LETTERS[1:10] #define nodes
nc = expand.grid(nodes,nodes) #connect all nodes with edges
nc = nc[-which(nc[,1] == nc[,2]),] #remove node connections to themselves
set.seed(666)
nc = cbind(nc, runif(nrow(nc),0,1)) #add random weights
ne = tibble(from = nc[,1], to = nc[,2], wt = as.numeric(nc[,3]))
g = graph_from_data_frame(ne) #create graph from nodes and edges
CairoWin() #open window with anti-aliased plot
ggraph(g, layout = "circle")
geom_edge_link(aes(alpha = wt), edge_width = 1) #add edges with weight
geom_node_point(size = 10, fill = "white", color = "black", shape = 21, stroke = 1.5)
geom_node_text(aes(label = name))
scale_edge_alpha_continuous(name = "Weight", seq(0,1,0.1))
theme_void()

非常感謝您的幫助!
uj5u.com熱心網友回復:
這是因為邊緣的 alpha 是在內部呼叫的edge_alpha,而alpha不像aes()你所相信的那樣。因此,您可以使用scale_edge_alpha_continuous()來定義圖例:
library(dplyr) #required for tibbles
library(igraph) #required for plotting
library(ggraph) #required for plotting
nodes = LETTERS[1:10] #define nodes
nc = expand.grid(nodes,nodes) #connect all nodes with edges
nc = nc[-which(nc[,1] == nc[,2]),] #remove node connections to themselves
set.seed(666)
nc = cbind(nc, runif(nrow(nc),0,1)) #add random weights
ne = tibble(from = nc[,1], to = nc[,2], wt = as.numeric(nc[,3]))
g = graph_from_data_frame(ne) #create graph from nodes and edges
p <- ggraph(g, layout = "circle")
geom_edge_link(aes(alpha = wt), edge_width = 1) #add edges with weight
geom_node_point(size = 10, fill = "white", color = "black", shape = 21, stroke = 1.5)
geom_node_text(aes(label = name))
theme_void()
p scale_edge_alpha_continuous(name = "Weight", seq(0,1,0.1),
guide = guide_legend(nrow = 3))

guides()或者,如果您命名正確的美學,您也可以使用函式指定它。
p scale_edge_alpha_continuous(name = "Weight", seq(0,1,0.1))
guides(edge_alpha = guide_legend(nrow = 3))

由reprex 包于 2022-01-21 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420825.html
標籤:
