Skip to content

Commit 3764c00

Browse files
authored
fix: correct mapping of edge label properties in plots when loops are present (#1706)
1 parent 5298f88 commit 3764c00

3 files changed

Lines changed: 110 additions & 6 deletions

File tree

R/plot.R

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ plot.igraph <- function(x,
292292
}
293293
}
294294

295-
loop <- function(x0, y0, cx = x0, cy = y0, color, angle = 0, label = NA,
295+
loop <- function(x0, y0, cx = x0, cy = y0, color, angle = 0, label = NA, label.color,
296+
label.font, label.family, label.cex,
296297
width = 1, arr = 2, lty = 1, arrow.size = arrow.size,
297298
arr.w = arr.w, lab.x, lab.y, loopSize = loop.size) {
298299
rad <- angle
@@ -337,8 +338,8 @@ plot.igraph <- function(x,
337338
}
338339

339340
text(lx, ly, label,
340-
col = edge.label.color, font = edge.label.font,
341-
family = edge.label.family, cex = edge.label.cex
341+
col = label.color, font = label.font,
342+
family = label.family, cex = label.cex
342343
)
343344
}
344345
}
@@ -371,10 +372,28 @@ plot.igraph <- function(x,
371372
if (length(arrow.size) > 1) {
372373
asize <- arrow.size[loops.e]
373374
}
375+
lcol <- edge.label.color
376+
if (length(lcol) > 1) {
377+
lcol <- lcol[loops.e]
378+
}
379+
lfam <- edge.label.family
380+
if (length(lfam) > 1) {
381+
lfam <- lfam[loops.e]
382+
}
383+
lfon <- edge.label.font
384+
if (length(lfon) > 1) {
385+
lfon <- lfon[loops.e]
386+
}
387+
lcex <- edge.label.cex
388+
if (length(lcex) > 1) {
389+
lcex <- lcex[loops.e]
390+
}
391+
374392
xx0 <- layout[loops.v, 1] + cos(la) * vs
375393
yy0 <- layout[loops.v, 2] - sin(la) * vs
376394
mapply(loop, xx0, yy0,
377-
color = ec, angle = -la, label = loop.labels, lty = lty,
395+
color = ec, angle = -la, label = loop.labels,
396+
label.color = lcol, label.family = lfam, label.font = lfon, label.cex = lcex, lty = lty,
378397
width = ew, arr = arr, arrow.size = asize, arr.w = arrow.width,
379398
lab.x = loop.labx, lab.y = loop.laby
380399
)
@@ -447,9 +466,27 @@ plot.igraph <- function(x,
447466
if (!is.null(elab.y)) {
448467
lc.y <- ifelse(is.na(elab.y), lc.y, elab.y)
449468
}
469+
470+
ecol <- edge.label.color
471+
if (length(ecol) > 1) {
472+
ecol <- ecol[nonloops.e]
473+
}
474+
efam <- edge.label.family
475+
if (length(efam) > 1) {
476+
efam <- efam[nonloops.e]
477+
}
478+
efon <- edge.label.font
479+
if (length(efon) > 1) {
480+
efon <- efon[nonloops.e]
481+
}
482+
ecex <- edge.label.cex
483+
if (length(ecex) > 1) {
484+
ecex <- ecex[nonloops.e]
485+
}
486+
450487
text(lc.x, lc.y,
451-
labels = edge.labels, col = edge.label.color,
452-
family = edge.label.family, font = edge.label.font, cex = edge.label.cex
488+
labels = edge.labels, col = ecol,
489+
family = efam, font = efon, cex = ecex
453490
)
454491
}
455492

Lines changed: 43 additions & 0 deletions
Loading

tests/testthat/test-plot.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,27 @@ test_that("rglplot() works", {
8585
expect_silent(rglplot(g))
8686
expect_silent(rglplot(g, edge.label = letters[1:ecount(g)]))
8787
})
88+
89+
test_that("label colors are correct when loops are present", {
90+
# check that Bug 157 is fixed
91+
skip_if_not_installed("vdiffr")
92+
g <- make_graph(c(1, 2, 1, 1, 2, 3), directed = FALSE)
93+
g$layout <- structure(
94+
c(
95+
1.17106961533433,
96+
1.63885278868168,
97+
2.10732892696401,
98+
3.91718168529106,
99+
2.87660789399794,
100+
1.83449260993935
101+
),
102+
dim = 3:2
103+
)
104+
cols <- c("red", "green", "blue")
105+
vdiffr::expect_doppelganger(
106+
"loop graph",
107+
function() {
108+
plot(g, edge.color = cols, edge.label.color = cols, edge.label = cols)
109+
}
110+
)
111+
})

0 commit comments

Comments
 (0)