从不同的数据创建图表
问题描述:
我需要帮助来创建图表。我解释得更好。从不同的数据创建图表
我创建了10个随机图,每个图都有N个节点。 我已经做了N = 10^3,10^4,10^5。 所以共有30张图。
他们每个人都找到了他们拥有的多重链接和selfloops的百分比。
现在我想创建一个单一的图表,显示节点数目的功能百分比。 因此,像:
所以我有一个3所列出: - listNets
含30个图 - listSelf
含selfloops 的百分比 - 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)
}
现在if我打印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
答
以你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
到一个因素。 - 不再需要为标签解析文本,因此可以删除该部分。
- 调整y值中的值和断点。
下面的代码:
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))
谢谢,我试过你的代码,它似乎几乎完美。我修改了主要信息,结果我得到了,你会知道帮助我吗? – marielle
@marielle你还可以包含你用来制作剧情的代码吗?仅仅看情节本身,很难说出什么问题。 – Jaap
当然(抱歉)。我编辑我的主要信息。 – marielle