Use star symbols with ggnet2
Closed this issue · 3 comments
hi Shuangbin,
awesome package, thanks for building and sharing it!
Do you think I could use these symbols with ggnet2?
It has an internal shape = 19
parameter, but the final plot is a ggplot object.
Thanks
Abel
Example
library(GGally)
library(network)
library(sna)
library(ggplot2)
# random graph
net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)
# vertex names
network.vertex.names(net) = letters[1:10]
ggnet2(net, node.size = 6, node.color = "black", edge.size = 1, edge.color = "grey", node.shape = 18)
I used the description in ggnet/Hacking into internal values to achieve it by overplotting as on a regular ggobj.
The ggnet2
was implemented using ggplot2
layers and grammar. And the point
layer was provided by geom_point
. If you want to use the points of ggstar
. It might need some non-standard process. You can replace the layer of geom_point
of the final plot of ggnet2
. You can refer to the following codes.
library(ggplot2) library(GGally)
library(network)
library(sna)
library(ggstar)
set.seed(123)
net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)
network.vertex.names(net) = letters[1:10]
p <- ggnet2(
net,
node.size = 6,
node.color = "black",
edge.size = 1,
edge.color = "grey",
node.shape = 18
)
# the node points is the second layer
p$layers
# Since the character (size, color, alpha, fill) of node points was not used to map some variables in this example
# It is not need to use the mapping aes.
new.star.point <- geom_star(fill="black", size=6, starshape=14)
p$layers[[2]] <- new.star.point
p
If the character of node points has been used to map some variables, the mapping should be used now.
net %v% "phono" = ifelse(letters[1:10] %in% c("a", "e", "i"), "vowel", "consonant")
p2 <- ggnet2(net, color = "phono")
p2$data
p2$layers
new.star.point2 <- geom_star(mapping=aes(fill=color, starshape=color), size=6)
p2$layers[[2]] <- new.star.point2
p2 + scale_starshape_manual(values=c(13, 14)) + scale_fill_manual(values=c("#00AED7", "#FD9347"))
But I don't think it is a good idea. ggraph
might be a better option.
library(ggraph)
library(tidygraph)
graph <- as_tbl_graph(net)
graph
p3 <- ggraph(graph, layout="kk") +
geom_edge_fan() +
geom_star(mapping=aes(x=x, y=y, fill=phono, starshape=phono), size=6)
p3 + scale_starshape_manual(values=c(13, 14)) + scale_fill_manual(values=c("#00AED7", "#FD9347"))
Fantastic, and very detailed, thank you!