簡體   English   中英

R中igraph頂點值的顏色條圖例

[英]Color bar legend for values on vertices of igraph in R

我是 R 的新手,我開始使用 igraph 進行圖形可視化。 下面的示例創建了一個由 10 個頂點組成的簡單網絡,並根據顏色值為它們着色(在這種情況下,為簡單起見,我將其設置為與頂點的 id 相同)。

library(igraph)
vertices <- 1:10
first <- 1:10
second <- c(2:10,1)
edges = cbind(first,second)
color = 1:10
net = graph_from_data_frame(edges,vertices=vertices ,directed=F )
V(net)$color = color
plot(net)

然而,從這個圖中不清楚哪些顏色對應哪些數字:

在此處輸入圖片說明

為了解決這個問題,我嘗試創建各種可以在文檔和在線中找到的圖例。 以下面的代碼為例:

legend("bottom", legend=levels(as.factor(color)), bty = "n", cex =
1.5, pt.cex = 3, pch=20, col = color , horiz = FALSE , inset = c(0.1,
-0.3)

但在這種情況下,結果是混亂的,模糊了圖片,並且不提供將節點上的值范圍映射到色譜的連續顏色條。 我能找到的其他選項也好不到哪里去。

在此處輸入圖片說明

你知道如何在圖片下方或右側以連續顏色條的形式制作圖例(這樣它就不會覆蓋它的任何部分)? 理想情況下,顏色條應該顯示整個連續的顏色光譜以及與顏色相對應的幾個值(至少是極端值)? 你碰巧知道如何實現這一目標嗎?

感謝您的幫助!

您應該查看kokkenbaker的這個答案,雖然它有點麻煩,但它可能正是您所需要的。 如何在 R 中添加帶有透視圖的顏色條

感謝 ealbsho93 我能夠產生以下解決方案。 它創建一個調色板,然后將圖形上頂點上的值映射到調色板並顯示它。 這並不簡單,但結果看起來好多了(見下文)

rm(list=ls())
library(igraph)
library(fields)

vertices <- 1:10
first <- 1:10
second <- c(2:10,1)
edges = cbind(first,second)
net = graph_from_data_frame(edges,vertices=vertices ,directed=F )

#Here we create a sample function on the vertices of the graph
color_num = 10:1
#create a color palette of the same size as the number of vertices.
jet.colors <- colorRampPalette( rainbow( length( unique(color_num) ) ) )
color_spectrum <- jet.colors( length( unique(color_num ) ) )
#and over here we map the pallete to the order of values on vertices
ordered <- order(color_num)
color <- vector(length = length(ordered),mode="double")
for ( i in 1:length(ordered) )
{
    color[ ordered[i] ] <- color_spectrum [ i ]
}
V(net)$color = color

#Display the graph and the legend.
plot(net)
image.plot(legend.only=T, zlim=range(color_num), col=color_spectrum )

在此處輸入圖片說明

如果有更好的解決方案,請告訴我。 否則,這個似乎可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM