繁体   English   中英

无法使用 R/ggraph 在圆形树状图上按组对线条进行着色

[英]Unable to color lines by group on a circular dendogram using R/ggraph

我使用 R 和ggraph package 创建了一个圆形树状图。我的标签和节点已按“组”正确着色。 但是,我不确定如何通过“颜色”(我的颜色列)为线条着色。 目前我可以将线条颜色更改为单一颜色(例如“红色”),但我不能按“颜色”列动态地为它们着色。

我的代码基于r-graph gallery 网站的代码。 正如您从我的数据文件中看到的那样,我尝试添加一个“颜色”列,然后在我的 ggraph 调用中调用它,但这给了我以下错误:

Error in `geom_edge_diagonal(colour = colors)`:
! Problem while setting up geom aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `rep()`:
! attempt to replicate an object of type 'closure'

这是我的代码:

library(ggraph)
library(igraph)
library(tidyverse)
library(RColorBrewer)

d1 = read.csv("~/data1.csv", sep=",")
d2 = read.csv("~/data2.csv", sep=",")

edges=rbind(d1, d2)

# create a vertices data.frame. One line per object of our hierarchy
vertices = data.frame(
  name = unique(c(as.character(edges$from), as.character(edges$to))) , 
  value = runif(78)
) 

# Let's add a column with the group of each name. It will be useful later to color points
vertices$group = edges$from[ match( vertices$name, edges$to, edges$colors ) ]

#Let's add information concerning the label we are going to add: angle, horizontal adjustement and potential flip
#calculate the ANGLE of the labels
vertices$id=NA
myleaves=which(is.na( match(vertices$name, edges$from, edges$colors) ))
nleaves=length(myleaves)
vertices$id[ myleaves ] = seq(1:nleaves)
vertices$angle= 90 - 360 * vertices$id / nleaves

# calculate the alignment of labels: right or left
# If I am on the left part of the plot, my labels have currently an angle < -90
vertices$hjust<-ifelse( vertices$angle < -90, 1, 0)

# flip angle BY to make them readable
vertices$angle<-ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle)

# Create a graph object
mygraph <- graph_from_data_frame( edges, vertices=vertices )

# Make the plot
ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
  geom_edge_diagonal(colour=colors) +
  scale_edge_colour_distiller(palette = "RdPu") +
  geom_node_text(aes(x = x*1.12, y=y*1.12, filter = leaf, label=name, angle = angle, colour=group, hjust=hjust), size=6) +
  geom_node_point(aes(filter = leaf, x = x*1.07, y=y*1.07, colour=group, alpha=.2, size=2)) +
  scale_colour_manual(values= rep( brewer.pal(7,"Paired") , 30)) +
  scale_size_continuous( range = c(0.1,17) ) +
  theme_void() +
  theme(
    legend.position="none",
    plot.margin=unit(c(0,0,0,0),"cm"),
  ) +
  expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))

以下是所用代码的两个最小示例:

数据1.csv

from,to,colors
origin,group1,color1
origin,group2,color1
origin,group3,color1
origin,group4,color2
origin,group5,color2
origin,group6,color3
origin,group7,color3
origin,group8,color4
origin,group9,color4

数据2.csv

from,to,colors
group1,"test1",color1
group2,"test2",color1
group3,"test3",color1
group4,"test4",color2
group5,"test5",color2
group6,"test6",color3
group7,"test7",color3
group8,"test8",color4
group9,"test9",color4

我相信以下行是我需要帮助的行(在我的 ggraph 调用中):

geom_edge_diagonal(colour=colors) +

如果有帮助,我的问题与询问如何在我使用的示例代码中按组为线条着色相同,取自r-graph gallery

任何帮助表示赞赏。

我之前的评论中建议使用 aes(),确实解决了您发现的错误。 你刚刚意识到你还有另一个问题。 我们通过从 scale_edge_color_distiller 更改为 scale_edge_color_brewer 来解决这个问题,因为蒸馏器正在寻找一个连续变量。

请注意,我还进行了设置,因此您的数据是可重现的。 当您提供数据集以支持所提出的问题时,请考虑在将来使用 dput()。


d1 <- tribble(
    ~from,~to,~colors,
"origin","group1","color1",
"origin","group2","color1",
"origin","group3","color1",
"origin","group4","color2",
"origin","group5","color2",
"origin","group6","color3",
"origin","group7","color3",
"origin","group8","color4",
"origin","group9","color4")

d2 <- tribble(
    ~from, ~to, ~colors,
"group1","test1","color1",
"group2","test2","color1",
"group3","test3","color1",
"group4","test4","color2",
"group5","test5","color2",
"group6","test6","color3",
"group7","test7","color3",
"group8","test8","color4",
"group9","test9","color4")



edges=rbind(d1, d2)

# create a vertices data.frame. One line per object of our hierarchy
vertices = data.frame(
    name = unique(c(as.character(edges$from), as.character(edges$to))) , 
    value = runif(19)
) 

# Let's add a column with the group of each name. It will be useful later to color points
vertices$group = edges$from[ match( vertices$name, edges$to, edges$colors ) ]

#Let's add information concerning the label we are going to add: angle, horizontal adjustement and potential flip
#calculate the ANGLE of the labels
vertices$id=NA
myleaves=which(is.na( match(vertices$name, edges$from, edges$colors) ))
nleaves=length(myleaves)
vertices$id[ myleaves ] = seq(1:nleaves)
vertices$angle= 90 - 360 * vertices$id / nleaves

# calculate the alignment of labels: right or left
# If I am on the left part of the plot, my labels have currently an angle < -90
vertices$hjust<-ifelse( vertices$angle < -90, 1, 0)

# flip angle BY to make them readable
vertices$angle<-ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle)

# Create a graph object
mygraph <- graph_from_data_frame( edges, vertices=vertices )

# Make the plot
ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
    geom_edge_diagonal(aes(colour=colors)) +
    #scale_edge_colour_distiller(palette = "RdPu") +
    scale_edge_colour_brewer(palette = "RdPu") +
    geom_node_text(aes(x = x*1.12, y=y*1.12, filter = leaf, label=name, angle = angle, colour=group, hjust=hjust), size=6) +
    geom_node_point(aes(filter = leaf, x = x*1.07, y=y*1.07, colour=group, alpha=.2, size=2)) +
    scale_colour_manual(values= rep( brewer.pal(7,"Paired") , 30)) +
    scale_size_continuous( range = c(0.1,17) ) +
    theme_void() +
    theme(
        legend.position="none",
        plot.margin=unit(c(0,0,0,0),"cm"),
    ) +
    expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM