Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions R/plot.common.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions man/plot.common.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions tests/testthat/_snaps/plot/label-rotate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading