我有一个未加权的图形,我想得到一个子图,它只包含节点和边,包含n个已知节点之间的最短路径.在这种情况下,3个节点(11,29和13是名称).
如何获得R中n个节点之间最短路径的子图?
library(ggraph)
library(igraph)
hs <- highschool[highschool$year == '1958',]
set.seed(11)
graph <- graph_from_data_frame(hs[sample.int(nrow(hs), 60),])
# plot using ggraph
ggraph(graph, layout = 'kk') +
geom_edge_fan() +
geom_node_text(aes(label = name))
Run Code Online (Sandbox Code Playgroud)
所需的输出将是以下绿色子图(或者关闭,我正在观察上面的图形并在视觉上挑选出子图的内容)忽略/移除其他节点和边缘.
字母"a"是否可以从网络图中与例如a fill
或colour
美学相关联的图例中删除ggraph
,如下面的简单示例中所示?
library(igraph)
library(ggraph)
g1 <- make_ring(6)
vertex_attr(g1) <- list(name = LETTERS[1:6], type =rep(c("typeA", "typeB", "typeC"), 2))
ggraph(g1) + geom_node_label(aes(label = name, fill = type)) +
geom_edge_diagonal() + theme_graph()
Run Code Online (Sandbox Code Playgroud)
在这种情况下geom_text
,show.legend = FALSE
解决它,
但添加show.legend = FALSE
内部geom_node_label()
,完全删除图例.
这有什么解决方案ggraph
吗?
绘制网络时,如果节点的标签也可以避开网络边缘,那将是很好的选择。例如,在下面的示例中,可以将所有标签移到网络外部。我已经尝试了几个软件包,但是到目前为止,还没有找到一种可行的方法。有办法吗?下面的例子:
library(ggraph)
library(tidygraph)
reprex <- tibble(to = sample(1:10, 100,replace=T),
from = sample(1:10, 100,replace=T)
) %>%
as_tbl_graph()
V(reprex)$label1 <- rep("label",10)
reprex_plot <- reprex %>%
ggraph() +
geom_node_point() +
geom_edge_link(color="grey")+
geom_node_text(aes(label=label1),repel=T,force=100)+
theme_bw()
reprex_plot
Run Code Online (Sandbox Code Playgroud)
除了这篇文章之外,我在这里提出了新的问题,如何在ggraph
.
以下是生成当前绘图的脚本:
## Packages
library(igraph)
library(tidygraph)
library(ggraph)
library(ggplot2)
library(tidyverse)
## Edge and node
edge <- data.frame(from=c(0,0,0,0,1,2,3),
to=c(0,1,2,3,0,0,0),
weight=c(1,3,1,1,3,1,1))
node <- data.frame(id=c(0,1,2,3),
p=c(9,1,0,0),
w=c(0,2,0,0),
s=c(0,1,1,1),
size=c(9,3,1,1),
gr=c(0,1,1,2))
## Load data frames as tbl_graph class
edge <- edge %>% mutate(from=from+1,to=to+1)
net <- tbl_graph(nodes=node,edges=edge,directed=TRUE)
## Set arrows
ar <- arrow(angle=30,length=unit(5,"mm"),ends="last",type="closed")
## Plot
ggraph(net,layout="graphopt") +
## Edges
geom_edge_link(aes(start_cap=circle(log(node1.size)+2,unit="native"),
end_cap=circle(log(node2.size)+2,unit="native"),
width=weight,label=weight),
position="identity",angle_calc="along",force_flip=TRUE,
label_dodge=unit(4.0,"mm"),label_push=unit(-0.4,"mm")) +
## Width scale
scale_edge_width(range=c(0.4,4),breaks=c(1:10),name="Movements\nbetween zones") +
## Add arrows separately
geom_edge_link(arrow=ar,aes(start_cap=circle(log(node1.size)+1,unit="native"),
end_cap=circle(log(node2.size)+1,unit="native"))) +
## Nodes
## Plot …
Run Code Online (Sandbox Code Playgroud) 我对 R 非常陌生,我需要 ggraph 库,但无法从 rstudio 控制台安装它。这是一条消息:
\n\n\n\n\ninstall.packages 中的警告:包 \xe2\x80\x98ggraph\xe2\x80\x99 不可用(适用于\n R 版本 3.3.2)
\n
还有其他安装方式吗?看起来这个图书馆仍然存在并蓬勃发展:
\n\n\n这里对我来说有点神秘。我只是想使用 ggraph 更改图形的边缘颜色,无论我使用什么颜色,边缘都会变成橙色。例如,在这里我尝试将 edge_color 设置为灰色:
library(igraph)
library(ggraph)
ring <- sample_k_regular(10, 2)
ggraph(ring) +
geom_edge_link(aes(edge_colour='grey')) +
geom_node_point(shape=21,color='black',size = 7,stroke=1,fill='white' )+
theme_graph()
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我有以下数据框。
library(tidyverse)
library(ggraph)
library(tidygraph)
df <- structure(list(from = c(3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L,
2L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L,
1L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 3L, 1L, 2L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 2L, 1L,
3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 3L, 1L, 1L, …
Run Code Online (Sandbox Code Playgroud) 我正在使用 ggraph R 包创建一个图表。我想使用 来标记所有节点geom_node_label()
,但这通常会造成重叠。解决这个问题的一种方法是使用参数repel=TRUE
。不幸的是,我似乎无法弄清楚如何使边缘指向标签已移动的位置。
解决这个问题的一种可能的方法可能是在制作图表之前进行排斥布局,但我也没有找到一种方法。
library(igraph)
library(ggraph)
# Create example graph
my_graph <- make_graph('Meredith')
V(my_graph)$Label <- 1:70
set.seed(2222) # reproducible layout
ggraph(my_graph) +
geom_edge_link() +
geom_node_label(aes(label = Label), repel=FALSE) +
theme_graph()
# Graph with repel = TRUE
set.seed(2222)
ggraph(my_graph) +
geom_edge_link() +
geom_node_label(aes(label = Label), repel=TRUE) +
theme_graph()
Run Code Online (Sandbox Code Playgroud)
以下是上述代码的绘图:
使用时ggraph
,有没有办法加粗边缘颜色的图例线?我试图覆盖但无济于事。这是一个例子:
library(tidyverse)
library(igraph)
library(ggraph)
set.seed(20190607)
#create dummy data
Nodes <- tibble(source = sample(letters, 8))
Edges <- Nodes %>%
mutate(target = source) %>%
expand.grid() %>%
#assign a random weight & color
mutate(weight = runif(nrow(.)),
color = sample(LETTERS[1:5], nrow(.), replace = TRUE)) %>%
#limit to a subset of all combinations
filter(target != source,
weight > 0.7)
#make the plot
Edges %>%
graph_from_data_frame(vertices = Nodes) %>%
ggraph(layout = "kk") +
#link width and color are dynamic
geom_edge_link(alpha = 0.5, aes(width = …
Run Code Online (Sandbox Code Playgroud) 我正在寻找将ggraph
对象转换为迭代力网络的示例或教程。
首先我尝试plotly
使用函数转换为对象plotly::ggplotly
。但似乎plotly
不能很好地处理这种转换并且错过了边缘。
但我发现network3D
,我可以将一个igraph
对象转换为一个network3D
对象,但这不是我想要的。而且这个包的功能太冗长了。无论如何,没有从ggraph
对象转换的函数。
所以,我的问题非常基本,但是...您知道创建交互式ggraph
网络的任何方法吗?
谢谢
ggraph ×10
r ×9
ggplot2 ×3
ggrepel ×2
igraph ×2
algorithm ×1
force-layout ×1
interactive ×1
package ×1
plotly ×1