颜色图例没有出现在ggnet2图中

时间:2016-10-29 15:29:04

标签: r ggplot2

我正在尝试绘制网络g

> g
 Network attributes:
  vertices = 866 
  directed = TRUE 
  hyper = FALSE 
  loops = FALSE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 5310 
    missing edges= 0 
    non-missing edges= 5310 

 Vertex attribute names: 
    color degree membership vertex.names 

 Edge attribute names not shown 

g node"会员资格"以下列频率发生:

> table(g %v% "membership")

  1   2   3   4   5 
 19  44  11 196 596 

当我使用优秀的ggnet2包和以下函数时,虽然documentation suggests a plot for node size, color, etc. is automatically generated(参见节点图例部分),但没有图例的图表会返回:

ggnet2(g_new, color = "membership", size = "degree") +
    guides(size = F)

enter image description here

1 个答案:

答案 0 :(得分:1)

我的猜测是你将数字顶点属性传递给ggnet2,然后将其理解为颜色值的向量。

简答:

将您的membership属性转换为character字符串,您就可以了。

答案很长:

比较下面示例中的两个图表以获得差异:

library(GGally)
library(ggplot2)
library(network)
library(sna)

g <- network(rgraph(10))
g %v% "membership" <- sample(1:5, 10, replace = TRUE)
g %v% "membership_string" <- as.character(g %v% "membership")
g %v% "degree" <- degree(g)

g
table(g %v% "membership")

## Your graph
ggnet2(g, color = "membership", size = "degree") +
  guides(size = FALSE)

## Your graph, with the membership variable as a string
ggnet2(g, color = "membership_string", size = "degree") +
  guides(size = FALSE)

如果您想在图中使用可区分的颜色而不是灰度级,您可能需要这样的颜色:

ggnet2(g, color = "membership_string", size = "degree", color.palette = "Set1") +
  guides(size = FALSE)

此外,我是否可以建议您尝试ggnetwork包,其中包含其他功能?