我需要帮助来创建图表。我解释得更好。
我创建了10个随机图,每个图都有N个节点。 我做了N = 10 ^ 3,10 ^ 4,10 ^ 5。 总共30个图表。
对于他们每个人,我都找到了他们拥有的多链路和自循环的百分比。
现在我想创建一个单独的图表,显示节点数量的函数百分比。 如下所示:
所以我有3个名单:
- listNets
包含30个图表
- listSelf
包含自循环的百分比
- listMul
包含多重链接的百分比
这就是我所做的:
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)
}
现在,如果我以这种方式打印listNets
,我会有一些奇怪的事情:
> 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>
...
相反,如果我打印其他两个列表(listSelf
和listMult
,一切正常)。
现在,我该如何绘制这些数据?
我读过数据帧,但我不明白如何在我的情况下使用它。 有人能帮帮我吗?
我试图通过手工在csv文件上写一个可能的结果表让我回来,并试图绘制它以确定我是否朝着正确的方向前进。
这是代码,这是结果。 注意:我手工创建的表格和我发明了百分比。
> 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
有些不对劲。
非常感谢
代码是:
# 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
是:
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
答案 0 :(得分:2)
以df
数据框作为起点,您可以分两步获得所需的结果:
1)使用 reshape2 将数据重新整形为长格式:
library(reshape2)
dflong <- melt(df, id.vars = 'N')
2)使用 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)
给出:
我使用了透明度(alpha = 0.7
),以便能够看到点重叠的位置。
回应你的评论和问题中的第二个例子:
您必须稍微更改 ggplot2 代码:
x
中的aes
变量更改为系数。以下代码:
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)
会给你:
使用过的数据:
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))