1

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

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

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

这是代码:

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() 函数是如何工作的,或者帮我弄清楚如何传递边缘颜色数据。

谢谢!

编辑: .csv 文件

当我修复它时它生成的图像color=rep(data_df_color_mapping, each=100)在此处输入图像描述

4

1 回答 1

0

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

于 2020-05-21T16:18:52.100 回答