Reputation: 1373
I have an object that contains multiple graphs named: graphs
I want to plot the degree distributions for these graphs in one plot. I tried the following code but I keep getting the following error: " 'x' is a list, but does not have components 'x' and 'y'"
Here is the code:
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")
Upvotes: 0
Views: 402
Reputation: 10825
plot()
cannot handle lists, go over the list one by one, after setting up the plot properly. I.e. something like this:
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"))
Upvotes: 1