我正在尝试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 文件