Reputation:
I need help to create a chart. I explain better.
I created 10 random graphs, each with N nodes. I have done that for N = 10^3, 10^4, 10^5. So in total 30 graphs.
To each of them I found the percentage of multilinks and selfloops they have.
Now I would like to create a single graph that shows the percentage in function of the number of nodes. So something like:
So I have a 3 lists:
- listNets
containing 30 graphs
- listSelf
containing the percentage of selfloops
- listMul
containing the percentage of multilinks
This is what I did:
listN <- c((10^3), (10^4), (10^5))
# list of networks
listNets <- vector(mode = "list", length = 0)
# list of percentage of selfloops
listSelf <- vector(mode = "list", length = 0)
#list of percentage of multilinks
listMul <- vector(mode = "list", length = 0)
...
for(N in listN) {
...
net <- graph_from_adjacency_matrix(adjmatrix = adjacency_matrix, mode = "undirected") # it's work, infact if I plot it i saw a correct networks
listNets <- c(listNets, net) # I add net to list of networks
x11()
plot(net, layout = layout.circle(net))
...
# I find self-loops e multilinks
netmatr <- as_adjacency_matrix(net, sparse = FALSE)
num_selfloops <- sum(diag(netmatr))
num_multilinks <- sum(netmatr > 1)
# I find percentage
per_self <- ((num_selfloops/num_vertices)*100)
per_mul <- ((num_multilinks/num_edges)*100)
listSelf <- c(listSelf, per_self)
listMul <- c(listMul, per_mul)
}
Now if I print listNets
in this way I have something strange:
> print(listNets)
[[1]]
[1] 9
[[2]]
[1] FALSE
[[3]]
[1] 7 6 3 8 8 8
[[4]]
[1] 0 1 2 4 5 7
[[5]]
[1] 2 1 0 3 4 5
[[6]]
[1] 0 1 2 3 4 5
[[7]]
[1] 0 0 0 0 1 1 1 2 3 6
[[8]]
[1] 0 1 2 3 3 4 5 5 6 6
[[9]]
[[9]][[1]]
[1] 1 0 1
[[9]][[2]]
named list()
[[9]][[3]]
list()
[[9]][[4]]
list()
[[10]]
<environment: 0x000000001a6284a8>
[[11]]
[1] 9
[[12]]
[1] FALSE
[[13]]
[1] 2 5 8 8 7 8
[[14]]
[1] 0 1 3 4 6 7
[[15]]
[1] 0 1 4 2 3 5
[[16]]
[1] 0 1 2 3 4 5
[[17]]
[1] 0 0 0 1 1 1 2 2 3 6
[[18]]
[1] 0 1 2 2 3 4 4 5 6 6
[[19]]
[[19]][[1]]
[1] 1 0 1
[[19]][[2]]
named list()
[[19]][[3]]
list()
[[19]][[4]]
list()
[[20]]
<environment: 0x000000001a859e28>
...
Instead if I print the other two lists (listSelf
and listMult
everything is ok).
Now, how can I plot this data?
I read about dataframes, but I don't understand how to use it in my case. Can someone help me please?
I tried to bring me back by writing a possible result table on a csv file by hand and tried to plot it to see if I was going in the right direction.
This is the code and this the result. Note: The table I created by hand and I invented the percentages.
> df <- read.csv("./table.csv", sep = ",") # read csv file
> df
N perSelf perMul
1 10^3 2 1
2 10^3 5 1
3 10^3 98 15
4 10^3 50 51
5 10^3 41 52
6 10^3 21 100
7 10^3 36 80
8 10^3 70 20
9 10^3 80 55
10 10^3 100 44
11 10^4 2 1
12 10^4 5 18
13 10^4 100 20
14 10^4 50 51
15 10^4 51 52
16 10^4 21 100
17 10^4 36 80
18 10^4 70 20
19 10^4 73 85
20 10^4 100 98
21 10^5 100 10
22 10^5 5 1
23 10^5 98 15
24 10^5 50 51
25 10^5 41 52
26 10^5 21 85
27 10^5 36 80
28 10^5 65 20
29 10^5 80 55
30 10^5 100 44
There is something wrong.
Thanks a lot
The code is:
# create a matrix from a list (list_all)
mat <- matrix(unlist(list_all),
unique(lengths(list_all)),
dimnames = list(NULL, c("N", "% selfloops", "% multilinks")))
# convert matrix to data frame
df <- as.data.frame(x = mat, row.names = NULL)
df
# plot
dflong <- melt(df, id.vars = 'N')
x11()
ggplot(dflong, aes(x = N, y = value, color = variable)) +
geom_point(size = 5, alpha = 0.7, position = position_dodge(width = 0.3)) +
scale_x_discrete(labels = parse(text = as.character(unique(dflong$N)))) +
scale_y_continuous('', breaks = seq(0, 100, 25), labels = paste(seq(0, 100, 25), '%')) +
scale_color_manual('', values = c('red', 'blue'),
labels = c('Percentage of selfloop','Percentage of multilinks')) +
theme_minimal(base_size = 14)
df
is:
N % selfloops % multilinks
1 10 11.111111 0.00000
2 10 11.111111 0.00000
3 10 0.000000 0.00000
4 20 0.000000 0.00000
5 20 0.000000 15.38462
6 20 0.000000 0.00000
7 30 3.448276 0.00000
8 30 3.448276 0.00000
9 30 0.000000 0.00000
Upvotes: 2
Views: 128
Reputation: 83225
Taking your df
dataframe as a starting point, you can get the desired result in two steps:
1) Reshape your data into long format with reshape2:
library(reshape2)
dflong <- melt(df, id.vars = 'N')
2) Plot the data with ggplot2:
library(ggplot2)
ggplot(dflong, aes(x = N, y = value, color = variable)) +
geom_point(size = 5, alpha = 0.7, position = position_dodge(width = 0.3)) +
scale_x_discrete(labels = parse(text = as.character(unique(dflong$N)))) +
scale_y_continuous('', breaks = seq(0,100,25), labels = paste(seq(0,100,25),'%')) +
scale_color_manual('', values = c('red','blue'),
labels = c('Percentage of selfloop','Percentage of multilinks')) +
theme_minimal(base_size = 14)
which gives:
I used a transparency (alpha = 0.7
) in order to be able to see where points overlap.
In response to your comment and the second example in the question:
You have to alter the ggplot2 code a bit:
x
variable in the aes
to a factor.The following code:
ggplot(dflong, aes(x = factor(N), y = value, color = variable)) +
geom_point(size = 5, alpha = 0.5, position = position_dodge(width = 0.3)) +
xlab('N') +
scale_y_continuous('', breaks = seq(0, 20, 5),
labels = paste(seq(0, 20, 5), '%'),
limits = c(0,20)) +
scale_color_manual('',
values = c('red', 'blue'),
labels = c('Percentage of selfloop','Percentage of multilinks')) +
theme_minimal(base_size = 14)
will give you:
Used data:
df <- structure(list(N = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("10^3", "10^4", "10^5"), class = "factor"),
perSelf = c(2L, 5L, 98L, 50L, 41L, 21L, 36L, 70L, 80L, 100L, 2L, 5L, 100L, 50L, 51L, 21L, 36L, 70L, 73L, 100L, 100L, 5L, 98L, 50L, 41L, 21L, 36L, 65L, 80L, 100L),
perMul = c(1L, 1L, 15L, 51L, 52L, 100L, 80L, 20L, 55L, 44L, 1L, 18L, 20L, 51L, 52L, 100L, 80L, 20L, 85L, 98L, 10L, 1L, 15L, 51L, 52L, 85L, 80L, 20L, 55L, 44L)),
.Names = c("N", "perSelf", "perMul"), class = "data.frame", row.names = c(NA, -30L))
Upvotes: 1