0

我有一个包含多个图形的对象,名为:graphs

我想在一个图中绘制这些图的度数分布。我尝试了以下代码,但我不断收到以下错误:“‘x’是一个列表,但没有组件‘x’和‘y’”

这是代码:

library(igraph)
getDD  <- function(graph, cumulative = FALSE, ...) {
  if (!is.igraph(graph)) {
    stop("Not a graph object")
  }
  cs <- degree(graph, ...)
  hi <- hist(cs, -1:max(cs), plot = FALSE)$density
  if (!cumulative) {
    res <- hi
  } else {
    res <- rev(cumsum(rev(hi)))
  }
  res
}

generateGraph <- function(x){
return (barabasi.game(100))  
}

# generate 5 graphs
graphs = lapply(1:5, generateGraph)
dDistributions = lapply(graphs, getDD)

plot(dDistributions,  xlab="degree", ylab="cumulative frequency", main="Degree distribution", type="o")
4

1 回答 1

1

plot()无法处理列表,请在正确设置绘图后一一查看列表。即是这样的:

xlim <- c(0, max(sapply(dDistributions, length)-1))
ylim <- c(0, max(unlist(dDistributions)))

plot(NA, type="n", xlim=xlim, ylim=ylim, xlab="degree", ylab="relative frequency")
sapply(dDistributions, function(x) lines(1:length(x)-1, x, type="l"))
于 2013-08-12T20:58:04.837 回答