我有一个包含多个图形的对象:图形
我想在一个图中绘制这些图的度分布。 我尝试了以下代码,但我不断收到以下错误:“'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")
答案 0 :(得分: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"))