简体   繁体   English

igraph:如何为边和顶点分配颜色渐变?

[英]igraph: How to assign a gradient of colors to edges and vertices?

I am trying to assign the same color gradient to both, edges and vertices on igraph using R .我正在尝试使用Rigraph上的边和顶点分配相同的颜色渐变。 The gradient should color from low to high counts in the V(data_graph)$Counts portion.渐变应在 V(data_graph)$Counts 部分从低计数到高计数着色。

Some reproducible data:一些可重现的数据:

library(igraph) 
data <- data.frame(Country = c("Africa", "Argentina", "Bolivia", "Chile", "France", "Guam", "Poland", "Greenland", "Switzerland", "UK", "US"), 
               Counts = c(2,2,8,1,5,3,3,15, 15,30,35), 
               Pillar = c(rep("SoS", 4), rep("CNES", 4), rep("STE", 3)))

data_center <- setNames(data.frame("c", 1, "Sos"), names(data))
data_vtx <- rbind(data_center, data)
data_rel <- data.frame(from = head(data_vtx$Country, 1), 
                    to = data_vtx$Country[2:12])

which produces the following graph:产生下图:

data_graph <- graph_from_data_frame(data_rel,  directed = FALSE, data_vtx)

plot(data_graph, 
 layout = layout.fruchterman.reingold(data_graph),
 vertex.size = round(V(data_graph)$Counts/1.5, 2), 
 vertex.label.color = "black",
 edge.curved = 0.3)

However, next I would like to apply the same color gradient to both, the vertex and the edges based on the V(data_graph)$Counts .但是,接下来我想根据V(data_graph)$Counts对顶点和边应用相同的颜色渐变。 What I need is something similar to the scale_color_gradient2 command from ggplot2 .我需要的是类似于 ggplot2 的scale_color_gradient2命令的ggplot2

Example:例子:

scale_color_gradient2(low = “blue”, mid = “yellow”, high = “red”, midpoint = mean(V(data_graph)$Counts))

The closest I have found is the following post: How can I assign color range to edges in igraph plot in R based on edge attributes?我发现最接近的是以下帖子: How can I assign color range to edges in igraph plot in R based on edge attributes? This post was very informative on making a color gradient in igraph , but each edge there has it's own color.这篇文章对在igraph中制作颜色渐变提供了很多信息,但那里的每条边都有自己的颜色。 Because some of the V(data_graph)$Counts are the same, they should be colored the same in the color gradient.因为一些V(data_graph)$Counts是相同的,所以它们在颜色渐变中的颜色应该相同。

Any help will be greatly appreciated!任何帮助将不胜感激!

You can use ColorRampPalette to create the color ramp from blue to yellow to red.您可以使用ColorRampPalette创建从蓝色到黄色再到红色的色带。 Then use the Counts to select the colors for both the vertices and the edges.然后使用计数来选择顶点和边的颜色。 I used a somewhat arbitrary way of selecting the colors, but you can adjust the aesthetics to your taste.我使用了一种有点随意的方式来选择颜色,但您可以根据自己的喜好调整美学。

CRP = colorRampPalette(c("blue", "yellow", "red"))

plot(data_graph, 
 layout = layout.fruchterman.reingold(data_graph),
 vertex.size = round(V(data_graph)$Counts/1.5, 2), 
 vertex.label.color = "black",
 vertex.color = CRP(19)[round(V(data_graph)$Counts/2)+1],
 edge.color   = CRP(19)[round(V(data_graph)$Counts/2)+1],
 edge.curved = 0.3)

带彩色边的图

Assuming your palette is known.假设您的调色板是已知的。 If not see Gradient of n colors ranging from color 1 and color 2 .如果看不到从颜色 1 到颜色 2 的 n 种颜色的渐变 Add the Palette to the data and data_center data frames.将 Palette 添加到datadata_center数据框中。 Add vertex and edge colors to the plot() statement.将顶点和边颜色添加到plot()语句。

library(igraph)

data <- data.frame(Country = c("Africa", "Argentina", "Bolivia", "Chile", "France", "Guam", "Poland", "Greenland", "Switzerland", "UK", "US"), 
               Counts = c(2,2,8,1,5,3,3,15, 15,30,35), 
               Pillar = c(rep("SoS", 4), rep("CNES", 4), rep("STE", 3)),
              Palette = rainbow(11)  # <<< 
         )

data_center <- setNames(data.frame("c", 1, "Sos", "black"), names(data)) # <<< center color  is black
data_vtx <- rbind(data_center, data)
data_rel <- data.frame(from = head(data_vtx$Country, 1), 
                         to = data_vtx$Country[2:12]
            )
                    
data_graph <- graph_from_data_frame(data_rel,  directed = FALSE, data_vtx)

set.seed(202212)
plot(data_graph, layout = layout_as_star(data_graph),
 vertex.size        = round(V(data_graph)$Counts/1.5, 2), 
 vertex.color       = data_vtx$Palette,
 vertex.label.color = "black",
 edge.width         = 3,
 edge.curved        = 0.3,
 edge.color         = data$Palette
) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM