ggraph中的长格式和宽格式等价物
我试图使图表与此类似:ggraph中的长格式和宽格式等价物,r,ggraph,R,Ggraph,我试图使图表与此类似: library(tidyverse) library(ggraph) iris_g <- den_to_igraph(as.dendrogram(hclust(dist(iris[1:4])))) xy_expand <- function(xy, expand) xy * (1 + expand / 10) ord <- match(as.numeric(V(iris_g)$label), seq_len(nrow(iris))) V(iris_g
library(tidyverse)
library(ggraph)
iris_g <- den_to_igraph(as.dendrogram(hclust(dist(iris[1:4]))))
xy_expand <- function(xy, expand) xy * (1 + expand / 10)
ord <- match(as.numeric(V(iris_g)$label), seq_len(nrow(iris)))
V(iris_g)$Petal.Length <- iris$Petal.Length[ord]
V(iris_g)$Petal.Width <- iris$Petal.Width[ord]
V(iris_g)$Sepal.Length <- iris$Sepal.Length[ord]
V(iris_g)$Sepal.Width <- iris$Sepal.Width[ord]
V(iris_g)$Species <- iris$Species[ord]
ggraph(iris_g, layout = "dendrogram", circular = TRUE) +
geom_edge_diagonal() +
geom_node_point(aes(filter = leaf,
x = xy_expand(x, Sepal.Length),
y = xy_expand(y, Sepal.Length)),
color = "red") +
geom_node_point(aes(filter = leaf,
x = xy_expand(x, Sepal.Width),
y = xy_expand(y, Sepal.Width)),
color = "green") +
geom_node_point(aes(filter = leaf,
x = xy_expand(x, Petal.Length),
y = xy_expand(y, Petal.Length)),
color = "blue") +
geom_node_point(aes(filter = leaf,
x = xy_expand(x, Petal.Width),
y = xy_expand(y, Petal.Width)),
color = "yellow")
使用
tidygraph
,将边和节点数据保持为长格式,作为tbl\u graph
类的一部分,然后您可以将颜色添加到geom\u node\u点
是否有一些示例代码,tidygraph的文档非常稀疏,iris\u g%%作为\u tbl\u graph%%激活(节点)%%>%(键=性状,值=值,萼片。长度:花瓣。宽度)
不起作用。
library(ggplot2)
iris %>%
mutate(id = seq_len(nrow(iris))) %>%
gather(key = Trait, value = Value, Sepal.Length:Petal.Width) %>%
ggplot(aes(x = id, y = Value, color = Trait)) +
geom_point()