geom_conn_bundle 中的颜色映射未正确显示

color mapping in geom_conn_bundle not showing correctly

我正在尝试使用 geom_conn_bundleggraph 中绘制 R 中的边缘捆绑图,但我很难弄清楚为什么颜色映射无法正常工作。

我有一个包含 152 行和 3 列的数据框:从、到和颜色。每行代表两个人之间的联系。

我使用 this 教程来构建我的主要绘图功能:

代码如下:

data_df <- read.csv('Final Natarajan - CGMRP_CCSG-Pubs_Fig-1-2.csv')
data_df_color_mapping <- data_df$color

data_df_color_mapping <- data_df_color_mapping[!(duplicated(data_df))]

data_df <- data_df[!(duplicated(data_df)),]
row.names(data_df) <- NULL
colnames(data_df) <- c('from', 'to')

hierarchy <- data.frame('from' = rep('origin', 71), 'to' = unique(c(as.character(data_df$from), 
as.character(data_df$to))))

vertices <- data.frame(name = unique(c(as.character(hierarchy$from), as.character(hierarchy$to))) ) 

mygraph <- graph_from_data_frame( hierarchy, vertices=vertices )

# The connection object must refer to the ids of the leaves:
from <- match( data_df$from, vertices$name)
to <- match( data_df$to, vertices$name)

vertices$id <- NA
myleaves <- which(is.na( match(vertices$name, hierarchy$from) ))
nleaves <- length(myleaves)
vertices$id[ myleaves ] <- seq(1:leaves)
vertices$angle <- 110 - 360 * vertices$id / leaves

# 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)

png(file="edge_bundle.png",width=360*10,height=335*10)

ggraph(mygraph, layout = 'dendrogram', circular = TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to), color=data_df_color_mapping, 
alpha=.3, width=2) +
  geom_node_text(aes(x = x*1.12, y=y*1.12, filter = leaf, label=name, angle=vertices$angle), size=16, 
alpha=1) +
  theme_void() +
  theme(
    legend.position="none",
    plot.margin=unit(c(0,0,0,0),"cm"),
  ) +
  expand_limits(x = c(-1.2, 1.2), y = c(-1.2, 1.2))

dev.off()

我对

有疑问

geom_conn_bundle(data = get_con(from = from, to = to), color=data_df_color_mapping, alpha=.3, width=2)

我收到以下错误:

Error: Aesthetics must be either length 1 or the same as the data (15200): edge_colour

我用 color=rep(data_df_color_mapping, each=100) 修复了它,但映射已关闭。我无法弄清楚模式。我很自然地假设我必须将每种颜色重复 100 次,因为我有 152 行,但事实并非如此。

我在将颜色放入 aes() 时也遇到了问题,在那种情况下我会得到以下错误

Error: Aesthetics must be either length 1 or the same as the data (456): edge_colour

现在它的数据点是我数据框中的 3 倍。

任何人都可以帮助我了解 get_con() 函数的工作原理或帮助我弄清楚如何传递边缘颜色数据。

谢谢!

编辑: The .csv file

我用color=rep(data_df_color_mapping, each=100)

修复它时生成的图像

我是随机想出来的。所以当我重新排序数据时,我忘记了更改颜色映射,我意识到它产生了相同的图。所以发生的事情是它根据 "from" 的顶点索引对图进行排序。当我预先订购图表时,它会产生所需的结果。