Skip to content

Commit 68cbd0a

Browse files
authored
Merge branch 'up' into set_size
2 parents 37e7b81 + 4c16357 commit 68cbd0a

File tree

5 files changed

+96
-30
lines changed

5 files changed

+96
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ gplot(h)
160160

161161
# Keyword Arguments
162162
+ `layout` Layout algorithm: `random_layout`, `circular_layout`, `spring_layout`, `shell_layout`, `stressmajorize_layout`, `spectral_layout`. Default: `spring_layout`
163+
+ `title` Plot title. Default: `""`
164+
+ `title_color` Plot title color. Default: `colorant"black"`
165+
+ `title_size` Plot title size. Default: `4.0`
166+
+ `font_family` Font family for all text. Default: `"Helvetica"`
163167
+ `NODESIZE` Max size for the nodes. Default: `3.0/sqrt(N)`
164168
+ `nodesize` Relative size for the nodes, can be a Vector. Default: `1.0`
165169
+ `nodelabel` Labels for the vertices, a Vector or nothing. Default: `nothing`

src/GraphPlot.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module GraphPlot
22

33
using Compose # for plotting features
44
using Graphs
5+
using LinearAlgebra
6+
using SparseArrays
57

68
const gadflyjs = joinpath(dirname(Base.source_path()), "gadfly.js")
79

src/layout.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ julia> locs_x, locs_y = spring_layout(g)
102102
```
103103
"""
104104
function spring_layout(g::AbstractGraph,
105-
locs_x=2*rand(nv(g)).-1.0,
106-
locs_y=2*rand(nv(g)).-1.0;
105+
locs_x::Vector{R1}=2*rand(nv(g)).-1.0,
106+
locs_y::Vector{R2}=2*rand(nv(g)).-1.0;
107107
C=2.0,
108108
MAXITER=100,
109-
INITTEMP=2.0)
109+
INITTEMP=2.0) where {R1 <: Real, R2 <: Real}
110110

111111
nvg = nv(g)
112112
adj_matrix = adjacency_matrix(g)
@@ -174,7 +174,7 @@ end
174174

175175
using Random: MersenneTwister
176176

177-
function spring_layout(g::AbstractGraph, seed::Integer, kws...)
177+
function spring_layout(g::AbstractGraph, seed::Integer; kws...)
178178
rng = MersenneTwister(seed)
179179
spring_layout(g, 2 .* rand(rng, nv(g)) .- 1.0, 2 .* rand(rng,nv(g)) .- 1.0; kws...)
180180
end
@@ -205,20 +205,20 @@ function shell_layout(g, nlist::Union{Nothing, Vector{Vector{Int}}} = nothing)
205205
if nv(g) == 1
206206
return [0.0], [0.0]
207207
end
208-
if nlist == nothing
208+
if isnothing(nlist)
209209
nlist = [collect(1:nv(g))]
210210
end
211211
radius = 0.0
212212
if length(nlist[1]) > 1
213213
radius = 1.0
214214
end
215-
locs_x = Float64[]
216-
locs_y = Float64[]
215+
locs_x = zeros(nv(g))
216+
locs_y = zeros(nv(g))
217217
for nodes in nlist
218218
# Discard the extra angle since it matches 0 radians.
219219
θ = range(0, stop=2pi, length=length(nodes)+1)[1:end-1]
220-
append!(locs_x, radius*cos.(θ))
221-
append!(locs_y, radius*sin.(θ))
220+
locs_x[nodes] = radius*cos.(θ)
221+
locs_y[nodes] = radius*sin.(θ)
222222
radius += 1.0
223223
end
224224
return locs_x, locs_y

src/lines.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,49 @@ function curveedge(x1, y1, x2, y2, θ, outangle, d; k=0.5)
201201

202202
return [(x1,y1) (xc1, yc1) (xc2, yc2) (x2, y2)]
203203
end
204+
205+
function build_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
206+
if arrowlengthfrac > 0.0
207+
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
208+
curves = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
209+
carrows = line(arrows_cord)
210+
else
211+
curves_cord = graphcurve(g, locs_x, locs_y, nodesize, outangle)
212+
curves = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
213+
carrows = nothing
214+
end
215+
216+
return curves, carrows
217+
end
218+
219+
function build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
220+
if arrowlengthfrac > 0.0
221+
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
222+
lines = line(lines_cord)
223+
larrows = line(arrows_cord)
224+
else
225+
lines_cord = graphline(g, locs_x, locs_y, nodesize)
226+
lines = line(lines_cord)
227+
larrows = nothing
228+
end
229+
230+
return lines, larrows
231+
end
232+
233+
function build_straight_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
234+
A = adjacency_matrix(g) #adjacency matrix
235+
B = spdiagm(diag(A)) #diagonal matrix (self-loops)
236+
A[diagind(A)] .= 0 #set diagonal elements to 0 (remove self-loops)
237+
if is_directed(g)
238+
g1 = SimpleDiGraph(A)
239+
g2 = SimpleDiGraph(B)
240+
else
241+
g1 = SimpleGraph(A)
242+
g2 = SimpleGraph(B)
243+
end
244+
245+
lines, larrows = build_straight_edges(g1, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
246+
curves, carrows = build_curved_edges(g2, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
247+
248+
return lines, larrows, curves, carrows
249+
end

src/plot.jl

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ Layout algorithm. Currently can be one of [`random_layout`,
2323
`spectral_layout`].
2424
Default: `spring_layout`
2525
26+
`title`
27+
Plot title. Default: `""`
28+
29+
`title_color`
30+
Plot title color. Default: `colorant"black"`
31+
32+
`title_size`
33+
Plot title size. Default: `4.0`
34+
35+
`font_family`
36+
Font family for all text. Default: `"Helvetica"`
37+
2638
`NODESIZE`
2739
Max size for the nodes. Default: `3.0/sqrt(N)`
2840
@@ -102,6 +114,10 @@ Padding for the plot margins. Default: `0mm`
102114
"""
103115
function gplot(g::AbstractGraph{T},
104116
locs_x_in::Vector{R1}, locs_y_in::Vector{R2};
117+
title = "",
118+
title_color = colorant"black",
119+
title_size = 4.0,
120+
font_family = "Helvetica",
105121
nodelabel = nothing,
106122
nodelabelc = colorant"black",
107123
nodelabelsize = 1.0,
@@ -217,37 +233,35 @@ function gplot(g::AbstractGraph{T},
217233
end
218234

219235
# Create lines and arrow heads
220-
lines, arrows = nothing, nothing
236+
lines, larrows = nothing, nothing
237+
curves, carrows = nothing, nothing
221238
if linetype == "curve"
222-
if arrowlengthfrac > 0.0
223-
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
224-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
225-
arrows = line(arrows_cord)
226-
else
227-
curves_cord = graphcurve(g, locs_x, locs_y, nodesize, outangle)
228-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
229-
end
239+
curves, carrows = build_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
240+
elseif has_self_loops(g)
241+
lines, larrows, curves, carrows = build_straight_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
230242
else
231-
if arrowlengthfrac > 0.0
232-
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
233-
lines = line(lines_cord)
234-
arrows = line(arrows_cord)
235-
else
236-
lines_cord = graphline(g, locs_x, locs_y, nodesize)
237-
lines = line(lines_cord)
238-
end
243+
lines, larrows = build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
239244
end
240245

246+
# Set plot_size
241247
if length(plot_size) != 2 || !isa(plot_size[1], Compose.AbsoluteLength) || !isa(plot_size[2], Compose.AbsoluteLength)
242248
error("`plot_size` must be a Tuple of lengths")
243249
end
244250
Compose.set_default_graphic_size(plot_size...)
245-
compose(context(units=UnitBox(-1.2, -1.2, +2.4, +2.4; leftpad, rightpad, toppad, bottompad)),
246-
compose(context(), texts, fill(nodelabelc), stroke(nothing), fontsize(nodelabelsize)),
251+
252+
# Fix title offset
253+
title_offset = isempty(title) ? 0 : 0.1*title_size/4
254+
255+
# Build figure
256+
compose(context(units=UnitBox(-1.2, -1.2 - title_offset, +2.4, +2.4 + title_offset; leftpad, rightpad, toppad, bottompad)),
257+
compose(context(), text(0, -1.2 - title_offset/2, title, hcenter, vcenter), fill(title_color), fontsize(title_size), font(font_family)),
258+
compose(context(), texts, fill(nodelabelc), fontsize(nodelabelsize), font(font_family)),
247259
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
248260
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
249-
compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)),
250-
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
261+
compose(context(), larrows, stroke(edgestrokec), linewidth(edgelinewidth)),
262+
compose(context(), carrows, stroke(edgestrokec), linewidth(edgelinewidth)),
263+
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)),
264+
compose(context(), curves, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
251265
end
252266

253267
function gplot(g; layout::Function=spring_layout, keyargs...)

0 commit comments

Comments
 (0)