11# Visualize communities as convex hulls
22
3+ # ' Stretch (or shrink) the convex hull for plotting
4+ # '
5+ # ' @param xy a convex hull
6+ # ' @param fac a factor to stretch (>1) or shrink (<1) the hull for plotting
7+ # '
8+ # ' @return a new point set
9+ # ' @export
10+ stretch_convex_hull = function (xy , fac ){
11+ cx = mean(xy [,1 ])
12+ cy = mean(xy [,2 ])
13+ xn = xy [,1 ] - cx
14+ yn = xy [,2 ] - cy
15+
16+ r = sqrt(xn ^ 2 + yn ^ 2 )
17+ theta = atan2(yn ,xn )
18+ sxy = cbind(
19+ r * fac * cos(theta ) + cx ,
20+ r * fac * sin(theta ) + cy
21+ )
22+ return (sxy )
23+ }
24+
25+ # Visualize communities as convex hulls
26+
327# ' Make a set of convex hulls to visualize the community
428# '
529# ' @param model a model defined as a compound [list]
6- # ' @param net a network object
30+ # ' @param graph a graphwork object
731# ' @param cut if !null, the number of communities for igraph::cut_at
832# ' @param clrs a set of colors
933# ' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
1034# '
11- # ' @return the network object with convex hulls attached
35+ # ' @return the graphwork object with convex hulls attached
1236# ' @export
13- make_convex_hulls = function (model , net , cut = NULL , clrs = NULL , f_color = viridis :: turbo ){
14- clusters = net $ walktrap_clusters
37+ make_convex_hulls = function (model , graph , cut = NULL , clrs = NULL , f_color = viridis :: turbo ){
38+ clusters = graph $ walktrap_clusters
1539 memix = if (is.null(cut )){
1640 igraph :: membership(clusters )
1741 } else {
1842 igraph :: cut_at(clusters , cut )
1943 }
20- if (net $ type == " b" ){
44+ if (graph $ type == " b" ){
2145 xy = model $ b
2246 }
23- if (net $ type == " q" ){
47+ if (graph $ type == " q" ){
2448 xy = model $ q
2549 }
26- if (net $ type == " bq" ){
50+ if (graph $ type == " bq" ){
2751 xy = with(model ,rbind(b , q ))
2852 }
29- net $ convex_hulls = list ()
53+ graph $ convex_hulls = list ()
3054 if (is.null(clrs )) clrs = f_color(max(memix ))
3155 for (i in 1 : max(memix )){
32- net $ convex_hulls [[i ]] = list ()
33- net $ convex_hulls [[i ]]$ xy = make_convex_hull_i(i , memix , xy )
34- net $ convex_hulls [[i ]]$ clr = clrs [i ]
56+ graph $ convex_hulls [[i ]] = list ()
57+ graph $ convex_hulls [[i ]]$ xy = make_convex_hull_i(i , memix , xy )
58+ graph $ convex_hulls [[i ]]$ clr = clrs [i ]
3559 }
36- return (net )
60+ return (graph )
3761}
3862
3963# ' Make the convex hull for the i^th community
@@ -47,17 +71,12 @@ make_convex_hulls = function(model, net, cut=NULL, clrs=NULL, f_color = viridis:
4771# ' @return a [list] with a hull
4872# ' @export
4973make_convex_hull_i = function (i , memix , xy ){
50- ixj = which(memix == i )
51- hpts <- grDevices :: chull(xy [ixj ,])
52- ixk = c(hpts , hpts [1 ])
53- hxy = xy [ixj [ixk ],]
54- return (hxy )
74+ ix = which(memix == i )
75+ return (make_convex_hull(xy [ix ,]))
5576}
5677
5778# ' Make the convex hull for the i^th community
5879# '
59- # ' @param i the i^th community
60- # ' @param memix the community membership indices
6180# ' @param xy the point set
6281# '
6382# ' @return the xy points that define a convex hull
@@ -74,16 +93,16 @@ make_convex_hull_xy = function(xy){
7493
7594# ' Add the convex hulls to a framed plot
7695# '
77- # ' @param net a network object
96+ # ' @param graph a graphwork object
7897# ' @param stretch make the hull slightly larger or slightly smaller
7998# ' @param lwd hull line width
8099# '
81100# ' @return invisible(NULL)
82101# ' @export
83- plot_convex_hulls = function (net , stretch = 1.1 , lwd = 2 ){
84- n = length(net $ convex_hulls )
102+ plot_convex_hulls = function (graph , stretch = 1.1 , lwd = 2 ){
103+ n = length(graph $ convex_hulls )
85104 for (i in 1 : n ){
86- with(net $ convex_hulls [[i ]],{
105+ with(graph $ convex_hulls [[i ]],{
87106 sxy = stretch_convex_hull(xy , stretch )
88107 graphics :: polygon(sxy [,1 ], sxy [,2 ], border = clr , lwd = lwd )
89108 })
@@ -111,72 +130,3 @@ add_convex_hulls = function(memix, xy, clrs, stretch=0.1, lwd=2, llty=1){
111130 return (invisible ())
112131}
113132
114-
115- # ' Stretch (or shrink) the convex hull for plotting
116- # '
117- # ' @param xy a convex hull
118- # ' @param fac a factor to stretch (>1) or shrink (<1) the hull for plotting
119- # '
120- # ' @return a new point set
121- # ' @export
122- stretch_convex_hull = function (xy , fac ){
123- cx = mean(xy [,1 ])
124- cy = mean(xy [,2 ])
125- xn = xy [,1 ] - cx
126- yn = xy [,2 ] - cy
127-
128- r = sqrt(xn ^ 2 + yn ^ 2 )
129- theta = atan2(yn ,xn )
130- sxy = cbind(
131- r * fac * cos(theta ) + cx ,
132- r * fac * sin(theta ) + cy
133- )
134- return (sxy )
135- }
136-
137-
138- # ' Make a set of convex hulls to visualize the community
139- # '
140- # ' @param model a model defined as a compound [list]
141- # ' @param net a network object
142- # ' @param cut if !null, the number of communities for igraph::cut_at
143- # ' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
144- # '
145- # ' @return the network object with convex hulls attached
146- # ' @export
147- mod_convex_hulls.i = function (model , i , cut = NULL , f_color = turbo , stretch = 0.1 , lwd = 2 ){
148- net = getNet.i(model , i )
149- mod_convex_hulls.i(model , net , cut , clr_scheme , stretch , lwd )
150- }
151-
152- # ' Make a set of convex hulls to visualize the community
153- # '
154- # ' @param model a model defined as a compound [list]
155- # ' @param net a network object
156- # ' @param cut if !null, the number of communities for igraph::cut_at
157- # ' @param clrs a list of colors
158- # ' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
159- # ' @param stretch a stretch factor for the hulls
160- # ' @param lwd the line width for plotting
161- # '
162- # ' @return the network object with convex hulls attached
163- # ' @export
164- mod_convex_hulls = function (model , net , cut = NULL , clrs = NULL , f_color = viridis :: turbo , stretch = 0.1 , lwd = 2 ){
165- clusters = net $ walktrap_clusters
166- memix = if (is.null(cut )){
167- membership(clusters )
168- } else {
169- cut_at(clusters , cut )
170- }
171- if (net $ type == " b" ){
172- xy = model $ b
173- }
174- if (net $ type == " q" ){
175- xy = model $ q
176- }
177- if (net $ type == " bq" ){
178- xy = with(model ,rbind(b , q ))
179- }
180- if (is.null(clrs )) clrs = f_color(max(memix ))
181- add_convex_hulls(memix , xy , clrs , stretch , lwd )
182- }
0 commit comments