From 02f11a9115fb8da4a7402f3d1fdd944bc3688c1d Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 27 Jun 2025 21:43:56 +0200 Subject: [PATCH 1/4] added label.angle and label.adj --- R/plot.common.R | 9 +++++++++ man/plot.common.Rd | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/R/plot.common.R b/R/plot.common.R index 06a3327b9b9..cbceb0e8faa 100644 --- a/R/plot.common.R +++ b/R/plot.common.R @@ -225,6 +225,13 @@ #' #' The default value is `black`. #' } +#' \item{label.angle}{ +#' The rotation of the vertex labels, in degrees. Corresponds to the `srt` parameter of [graphics::text()]. +#' } +#' \item{label.adj}{ +#' one or two numeric values, giving the horizontal and vertical adjustment of the vertex labels. See also `adj` in [graphics::text()]. +#' } + #' \item{size.scaling}{ #' Switches between absolute vertex sizing (FALSE,default) and relative (TRUE). #' If FALSE, `vertex.size` and `vertex.size2` are used as is. @@ -4861,6 +4868,8 @@ i.vertex.default <- list( label.family = "serif", label.font = 1, label.cex = 1, + label.angle = 0, + label.adj = NULL, frame.color = "black", frame.width = 1, shape = "circle", diff --git a/man/plot.common.Rd b/man/plot.common.Rd index 99c01689b73..ebf6147a22e 100644 --- a/man/plot.common.Rd +++ b/man/plot.common.Rd @@ -234,6 +234,12 @@ discussed earlier for the possible values. The default value is \code{black}. } +\item{label.angle}{ +The rotation of the vertex labels, in degrees. Corresponds to the \code{srt} parameter of \code{\link[graphics:text]{graphics::text()}}. +} +\item{label.adj}{ +one or two numeric values, giving the horizontal and vertical adjustment of the vertex labels. See also \code{adj} in \code{\link[graphics:text]{graphics::text()}}. +} \item{size.scaling}{ Switches between absolute vertex sizing (FALSE,default) and relative (TRUE). If FALSE, \code{vertex.size} and \code{vertex.size2} are used as is. From 4d55762964bee4bcc409231306a1a9d6a289b879 Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 27 Jun 2025 21:44:15 +0200 Subject: [PATCH 2/4] added label.angle and adj and refactored vertex label plotting --- R/plot.R | 58 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/R/plot.R b/R/plot.R index 1421f7e5e08..c22dd0ad16a 100644 --- a/R/plot.R +++ b/R/plot.R @@ -112,6 +112,8 @@ plot.igraph <- function( label.degree <- params("vertex", "label.degree") label.color <- params("vertex", "label.color") label.dist <- params("vertex", "label.dist") + label.angle <- params("vertex", "label.angle") + label.adj <- params("vertex", "label.adj") labels <- params("vertex", "label") shape <- igraph.check.shapes(params("vertex", "shape")) @@ -724,30 +726,42 @@ plot.igraph <- function( y <- layout[, 2] + label.dist * sin(-label.degree) * (vertex.size + 6 * 8 * log10(2)) / 200 if (vc > 0) { - if (length(label.family) == 1) { - text( - x, - y, - labels = labels, - col = label.color, - family = label.family, - font = label.font, - cex = label.cex - ) - } else { - if1 <- function(vect, idx) if (length(vect) == 1) vect else vect[idx] - sapply(seq_len(vcount(graph)), function(v) { + nv <- vcount(graph) + + # Replicate all label attributes to length nv + label.col <- rep(label.color, length.out = nv) + label.fam <- rep(label.family, length.out = nv) + label.fnt <- rep(label.font, length.out = nv) + label.cex <- rep(label.cex, length.out = nv) + label.ang <- rep(label.angle, length.out = nv) + label.adj <- rep(list(label.adj), length.out = nv) + label.text <- rep(labels, length.out = nv) + + # Draw vertex labels + invisible(mapply( + function(x0, y0, lbl, col, fam, fnt, cex, srt, adj) { text( - x[v], - y[v], - labels = if1(labels, v), - col = if1(label.color, v), - family = if1(label.family, v), - font = if1(label.font, v), - cex = if1(label.cex, v) + x0, + y0, + labels = lbl, + col = col, + family = fam, + font = fnt, + cex = cex, + srt = srt, + adj = adj ) - }) - } + }, + x, + y, + label.text, + label.col, + label.fam, + label.fnt, + label.cex, + label.ang, + label.adj + )) } rm(x, y) invisible(NULL) From 1170d0707ceb8f5b88d886102683a8bfcc792381 Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 27 Jun 2025 21:47:42 +0200 Subject: [PATCH 3/4] added test --- tests/testthat/_snaps/plot/label-rotate.svg | 45 +++++++++++++++++++++ tests/testthat/test-plot.R | 17 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/testthat/_snaps/plot/label-rotate.svg diff --git a/tests/testthat/_snaps/plot/label-rotate.svg b/tests/testthat/_snaps/plot/label-rotate.svg new file mode 100644 index 00000000000..7ccc0490c08 --- /dev/null +++ b/tests/testthat/_snaps/plot/label-rotate.svg @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +AAAAA +BBBBB +CCCCC +DDDDD +EEEEE + + diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index d444050058f..e500fe6c77d 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -131,3 +131,20 @@ test_that("Edges stop at outside of rectangle node", { vdiffr::expect_doppelganger("rectangle-edges", rectangle_edges) }) + +test_that("Vertex label rotation works", { + skip_if_not_installed("vdiffr") + + label_rotate <- function() { + g <- make_ring(5, directed = FALSE, circular = FALSE) + V(g)$label <- c("AAAAA", "BBBBB", "CCCCC","DDDDD", "EEEEE") + g$layout <- cbind(1:5, rep(1, 5)) + plot( + g, + vertex.label.angle = c(90, 90, 270, 270, 90), + vertex.label.adj = c(1.1,0.5) + ) + } + + vdiffr::expect_doppelganger("label-rotate", label_rotate) +}) From 28213a357f3c2c6bc7e0886050d2a74751a09f72 Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 27 Jun 2025 21:50:39 +0200 Subject: [PATCH 4/4] dont calculate nv because vc exists --- R/plot.R | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/R/plot.R b/R/plot.R index c22dd0ad16a..2f5d0f329ec 100644 --- a/R/plot.R +++ b/R/plot.R @@ -726,16 +726,13 @@ plot.igraph <- function( y <- layout[, 2] + label.dist * sin(-label.degree) * (vertex.size + 6 * 8 * log10(2)) / 200 if (vc > 0) { - nv <- vcount(graph) - - # Replicate all label attributes to length nv - label.col <- rep(label.color, length.out = nv) - label.fam <- rep(label.family, length.out = nv) - label.fnt <- rep(label.font, length.out = nv) - label.cex <- rep(label.cex, length.out = nv) - label.ang <- rep(label.angle, length.out = nv) - label.adj <- rep(list(label.adj), length.out = nv) - label.text <- rep(labels, length.out = nv) + label.col <- rep(label.color, length.out = vc) + label.fam <- rep(label.family, length.out = vc) + label.fnt <- rep(label.font, length.out = vc) + label.cex <- rep(label.cex, length.out = vc) + label.ang <- rep(label.angle, length.out = vc) + label.adj <- rep(list(label.adj), length.out = vc) + label.text <- rep(labels, length.out = vc) # Draw vertex labels invisible(mapply(