Reputation: 33
Newbie to R here. I am trying to plot a network diagram with the code shown below. But when I specify the vertex size, the edges disappear. The with and without vertex specification and the resultant plot is shown below.
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.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 With vertex size specified
#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.sstatic.net/jH1oN.png
i'm not sure why when I insert the vertex size, the edges disappears. But here's the warning message that is shown:
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
Also, here is 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
Any suggestions please?
Upvotes: 3
Views: 730
Reputation: 887741
We can match with the attributes names of vertex and create a named vector for the 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)
-plot
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"))
Upvotes: 1