弧形图中的边缘着色

问题描述 投票:0回答:1

我有一个包含 3 列的数据框,我创建了一个弧形图(源 x 目标)。 我不知道如何用 st_disease 给我的边缘着色。

这是我的代码:

links <- data.frame(
source = c("Convalescent plasma", "Convalescent whole blood", "Gentamicin", "Interferon β-1a", "Lopinavir/ritonavir and recombinant IFN-β1b", "MAb114", "Plasma", "Plasma", "REGN-EB3", "Remdesivir", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Tecovirimat", "ZMapp"),
target = c("No active treatment", "No active treatment", "Doxycycline", "No active treatment", "No active treatment", "ZMapp", "No active treatment", "No active treatment", "ZMapp", "ZMapp", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "Favipiravir"),
st_disease = c("Ebola", "Ebola", "Plague", "Ebola", "MERS", "Ebola", "Lassa fever", "Lassa fever", "Ebola", "Ebola", "Nipah", "Crimean Congo Haemorrhagic Fever", "Crimean Congo Haemorrhagic Fever", "Lassa fever", "Lassa fever", "Lassa fever", "Mpox", "Ebola")
)

mygraph <- graph_from_data_frame(links)
p2 <-  ggraph(mygraph, layout="linear") +
geom_edge_arc0(edge_colour="#69b3a2",
edge_alpha=0.3,
edge_width=1.5) +
geom_node_point( color="#69b3a2", size=5, position = "identity") +
geom_node_text( aes(label=name), repel = TRUE, size=4, color="black", nudge_y=-0.5 ,angle=70, vjust=1) +
scale_edge_color_manual(values = col)+
theme_void() +
theme(
legend.position="none",
# plot.margin=unit(rep(2,4), "cm")
plot.margin=unit(c(4,2,2,2), "cm"))+

coord_cartesian(clip = "off")

p2

我尝试更改edge_colour,但我不明白为什么它要求长度为72的美学。

r colors arc-diagram
1个回答
0
投票

我不确定你的代码中的

scale_edge_color_manual(values = col)
应该是什么。假设它是定义调色板的向量,我没有看到它被定义。您可以先创建调色板,然后使用
scale_edge_color_manual
在绘图中使用该调色板。

library(ggraph)


links <- data.frame(
  source = c("Convalescent plasma", "Convalescent whole blood", "Gentamicin", "Interferon β-1a", 
             "Lopinavir/ritonavir and recombinant IFN-β1b", "MAb114", "Plasma", "Plasma", "REGN-EB3", 
             "Remdesivir", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", 
             "Tecovirimat", "ZMapp"), 
  target = c("No active treatment", "No active treatment", "Doxycycline", "No active treatment", 
             "No active treatment", "ZMapp", "No active treatment", "No active treatment", "ZMapp", 
             "ZMapp", "No active treatment", "No active treatment", "No active treatment", 
             "No active treatment", "No active treatment", "No active treatment", "No active treatment", 
             "Favipiravir"), 
  st_disease = c("Ebola", "Ebola", "Plague", "Ebola", "MERS", "Ebola", "Lassa fever", "Lassa fever", 
                 "Ebola", "Ebola", "Nipah", "Crimean Congo Haemorrhagic Fever", 
                 "Crimean Congo Haemorrhagic Fever", "Lassa fever", "Lassa fever", "Lassa fever", 
                 "Mpox", "Ebola")
)

mygraph <- graph_from_data_frame(links)

# Create a color palette for the st_disease column
disease_colors <- setNames(
  c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"),
  unique(links$st_disease)
)


p2 <- ggraph(mygraph, layout = "linear") +
  geom_edge_arc(aes(edge_colour = st_disease), edge_width = 1.5, edge_alpha = 0.3) +
  geom_node_point(color = "#69b3a2", size = 5) +
  geom_node_text(aes(label = name), repel = TRUE, size = 4, color = "black", 
                 nudge_y = -0.5, angle = 70, vjust = 1) +
  scale_edge_color_manual(values = disease_colors) +
  theme_void() +
  theme(legend.position = "right", plot.margin = unit(c(4, 2, 2, 2), "cm")) +
  coord_cartesian(clip = "off")

p2
© www.soinside.com 2019 - 2024. All rights reserved.