diff --git a/R/plot.R b/R/plot.R index 47fec471a77..0b1e3f72ea8 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,39 @@ 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) { + 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( + 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) diff --git a/R/plot.common.R b/R/plot.common.R index 6350f1a683f..5f055c1fb0d 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. @@ -4847,6 +4854,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 b9b4fb750d9..36a2f9ca91d 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. 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 171f6bb7568..ed33e06f20d 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -132,7 +132,26 @@ 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) +}) + test_that("Arrow drawing works correctly", { + skip_if_not_installed("vdiffr") + standard_arrow <- function() { g <- make_graph(c(1, 2, 1, 3, 2, 4), directed = TRUE) g$layout <- cbind(1:4, rep(0, 4))