R的新手。我正在尝试使用下面显示的代码绘制网络图。但是当我指定顶点大小时,边缘消失了。有和没有顶点规范以及结果图如下所示。
没有顶点大小:
#NSW
clean_nsw <- clean %>%
filter(State=="NSW")
clean_nsw$CountryID.Origin <- str_to_title(clean_nsw$CountryID.Origin)
nsw_plot <- graph.data.frame(d=clean_nsw, directed=T)
plot.igraph(nsw_plot,
edge.color='grey23',
vertex.color='deepskyblue2',
edge.width=1,
vertex.label.color='gray0',
vertex.label.cex=0.75,
vertex.label.family="Helvetica",
vertex.label.font=2,
arrow.width=(clean_nsw$s_count*1),
layout= layout_nicely)
without vertex size 指定顶点大小
#NSW
clean_nsw <- clean %>%
filter(State=="NSW")
clean_nsw$CountryID.Origin <- str_to_title(clean_nsw$CountryID.Origin)
nsw_plot <- graph.data.frame(d=clean_nsw, directed=T)
plot.igraph(nsw_plot,
edge.color='grey23',
vertex.color='deepskyblue2',
edge.width=1,
vertex.size= clean_nsw$s_count*0.5,
vertex.label.color='gray0',
vertex.label.cex=0.75,
vertex.label.family="Helvetica",
vertex.label.font=2,
arrow.width=(clean_nsw$s_count*1),
layout= layout_nicely)
[image] https://i.stack.imgur.com/jH1oN.png
我不确定为什么插入顶点大小时边缘会消失。但是这是显示的警告消息:
2: In layout[, 1] + label.dist * cos(-label.degree) * (vertex.size + :
longer object length is not a multiple of shorter object length
3: In layout[, 2] + label.dist * sin(-label.degree) * (vertex.size + :
longer object length is not a multiple of shorter object length
此外,这里是clean_nsw
# A tibble: 6 x 3
# Groups: State [1]
CountryID.Origin State s_count
<chr> <chr> <int>
1 Thailand NSW 67
2 China NSW 51
3 Singapore NSW 43
4 Indonesia NSW 36
5 Fiji NSW 32
6 Malaysia NSW 32
有什么建议吗?
答案 0 :(得分:0)
我们可以匹配顶点的属性名称,并为vertex.size
library(igraph)
nm1 <- setNames(clean_nsw$s_count, clean_nsw$CountryID.Origin)
nm2 <- nm1[attr(V(nsw_plot), "names")]
names(nm2)[is.na(nm2)] <- "NSW"
nm2[is.na(nm2)] <- 10 # some value
plot.igraph(nsw_plot,
edge.color='grey23',
vertex.color='deepskyblue2',
edge.width=1,
vertex.size= nm2 * 0.5,
vertex.label = V(g)$names,
vertex.label.color='gray0',
vertex.label.cex=0.75,
vertex.label.family="Helvetica",
vertex.label.font=2,
arrow.width=nm2 * 1,
layout= layout_nicely)
-情节
clean_nsw <- structure(list(CountryID.Origin = c("Thailand", "China", "Singapore",
"Indonesia", "Fiji", "Malaysia"), State = c("NSW", "NSW", "NSW",
"NSW", "NSW", "NSW"), s_count = c(67L, 51L, 43L, 36L, 32L, 32L
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5",
"6"))