无法在R/ggraph中使用组对环形树状图进行线条着色。

huangapple go评论79阅读模式
英文:

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

问题

我已经创建了一个使用R和ggraph包的圆形树状图。我已经通过"group"正确着色了我的标签和节点。然而,我不确定如何通过"colors"(我的颜色列)来着色线条。目前,我可以将线条颜色更改为单一颜色(例如"red"),但我无法根据"colors"列动态着色它们。

我的代码基于r-graph gallery网站上的代码。从我的数据文件中可以看出,我尝试添加了一个"colors"列,然后在我的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 adjustment, 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") +
  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))

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

data1.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

data2.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(aes(colour=colors)) +

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

感谢您的帮助。

英文:

I've created a circular dendogram using R and the ggraph package. I have my labels and nodes correctly colored by "group". However, I'm unsure how to color the lines by "colors" (my color column). Currently I can change the line color to a single color (e.g. "red"), though I can't color them dynamically by column "colors".

My code is based on code from the r-graph gallery website. As you can see from my datafiles, I've tried adding a "colors" column and then calling that in my ggraph call but that gives me the following error:

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 &#39;closure&#39;

Here is my code:

library(ggraph)
library(igraph)
library(tidyverse)
library(RColorBrewer)
d1 = read.csv(&quot;~/data1.csv&quot;, sep=&quot;,&quot;)
d2 = read.csv(&quot;~/data2.csv&quot;, sep=&quot;,&quot;)
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&#39;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&#39;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 &lt; -90
vertices$hjust&lt;-ifelse( vertices$angle &lt; -90, 1, 0)
# flip angle BY to make them readable
vertices$angle&lt;-ifelse(vertices$angle &lt; -90, vertices$angle+180, vertices$angle)
# Create a graph object
mygraph &lt;- graph_from_data_frame( edges, vertices=vertices )
# Make the plot
ggraph(mygraph, layout = &#39;dendrogram&#39;, circular = TRUE) + 
geom_edge_diagonal(colour=colors) +
scale_edge_colour_distiller(palette = &quot;RdPu&quot;) +
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,&quot;Paired&quot;) , 30)) +
scale_size_continuous( range = c(0.1,17) ) +
theme_void() +
theme(
legend.position=&quot;none&quot;,
plot.margin=unit(c(0,0,0,0),&quot;cm&quot;),
) +
expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))

Here are two minimal examples of the code used:

data1.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

data2.csv

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

I believe the following line is the one I need help with (inside my ggraph call):

geom_edge_diagonal(colour=colors) +

If it helps, my question is the same as asking how to color lines by group on the example code I used, taken from r-graph gallery.

Any help is appreciated.

答案1

得分: 1

我之前的评论建议使用aes(),确实解决了您所识别的错误。您刚刚意识到另一个问题。我们可以通过从scale_edge_color_distiller更改为scale_edge_color_brewer来解决这个问题,因为distiller正在寻找一个连续的变量。

注意,我还设置了您的数据可再生性。在将数据集提供给支持问题时,考虑在未来使用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)

# 创建一个顶点数据框。每个层次结构对象一行
vertices = data.frame(
    name = unique(c(as.character(edges$from), as.character(edges$to))) , 
    value = runif(19)
) 

# 添加一个列,表示每个名称的组。这将在以后对点进行着色时很有用
vertices$group = edges$from[ match( vertices$name, edges$to, edges$colors ) ]

# 添加有关要添加的标签的信息:角度、水平调整和潜在翻转
# 计算标签的角度
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

# 计算标签的对齐方式:右对齐或左对齐
# 如果我在绘图的左侧部分,我的标签当前具有角度 < -90
vertices$hjust<-ifelse( vertices$angle < -90, 1, 0)

# 通过翻转角度使它们可读
vertices$angle<-ifelse(vertices$angle < -90, vertices$angle+180, vertices$angle)

# 创建图形对象
mygraph <- graph_from_data_frame( edges, vertices=vertices )

# 制作图形
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))

无法在R/ggraph中使用组对环形树状图进行线条着色。


[1]: https://i.stack.imgur.com/2FBTT.png
英文:

The suggestion in my earlier comments, to use aes(), did solve for the error you had identified. You just realized you had yet another problem. We solve for that by changing from scale_edge_color_distiller to scale_edge_color_brewer as distiller is looking for a continuous variable.

Note, I've also set up so your data is reproducible. Consider using dput() in the future when you provide your data set to support question asked.


d1 &lt;- tribble(
~from,~to,~colors,
&quot;origin&quot;,&quot;group1&quot;,&quot;color1&quot;,
&quot;origin&quot;,&quot;group2&quot;,&quot;color1&quot;,
&quot;origin&quot;,&quot;group3&quot;,&quot;color1&quot;,
&quot;origin&quot;,&quot;group4&quot;,&quot;color2&quot;,
&quot;origin&quot;,&quot;group5&quot;,&quot;color2&quot;,
&quot;origin&quot;,&quot;group6&quot;,&quot;color3&quot;,
&quot;origin&quot;,&quot;group7&quot;,&quot;color3&quot;,
&quot;origin&quot;,&quot;group8&quot;,&quot;color4&quot;,
&quot;origin&quot;,&quot;group9&quot;,&quot;color4&quot;)
d2 &lt;- tribble(
~from, ~to, ~colors,
&quot;group1&quot;,&quot;test1&quot;,&quot;color1&quot;,
&quot;group2&quot;,&quot;test2&quot;,&quot;color1&quot;,
&quot;group3&quot;,&quot;test3&quot;,&quot;color1&quot;,
&quot;group4&quot;,&quot;test4&quot;,&quot;color2&quot;,
&quot;group5&quot;,&quot;test5&quot;,&quot;color2&quot;,
&quot;group6&quot;,&quot;test6&quot;,&quot;color3&quot;,
&quot;group7&quot;,&quot;test7&quot;,&quot;color3&quot;,
&quot;group8&quot;,&quot;test8&quot;,&quot;color4&quot;,
&quot;group9&quot;,&quot;test9&quot;,&quot;color4&quot;)
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&#39;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&#39;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 &lt; -90
vertices$hjust&lt;-ifelse( vertices$angle &lt; -90, 1, 0)
# flip angle BY to make them readable
vertices$angle&lt;-ifelse(vertices$angle &lt; -90, vertices$angle+180, vertices$angle)
# Create a graph object
mygraph &lt;- graph_from_data_frame( edges, vertices=vertices )
# Make the plot
ggraph(mygraph, layout = &#39;dendrogram&#39;, circular = TRUE) + 
geom_edge_diagonal(aes(colour=colors)) +
#scale_edge_colour_distiller(palette = &quot;RdPu&quot;) +
scale_edge_colour_brewer(palette = &quot;RdPu&quot;) +
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,&quot;Paired&quot;) , 30)) +
scale_size_continuous( range = c(0.1,17) ) +
theme_void() +
theme(
legend.position=&quot;none&quot;,
plot.margin=unit(c(0,0,0,0),&quot;cm&quot;),
) +
expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))

无法在R/ggraph中使用组对环形树状图进行线条着色。

huangapple
  • 本文由 发表于 2023年1月9日 09:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定