Skip to content

Commit

Permalink
add missing step to assign values to 'node.text'
Browse files Browse the repository at this point in the history
  • Loading branch information
whelena committed Oct 4, 2024
1 parent 77a3eb5 commit b0b6440
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions R/add.text.R
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,30 @@ add.text2 <- function(
node.radius <- node.radius / scale;
node.text <- node.text[node.text$node %in% tree$tip, ];

node.list <- data.frame(row.names = rownames(tree));
node.text.col <- node.text.fontface <- lapply
node.list <- vector("list", nrow(tree));

Check warning on line 500 in R/add.text.R

View workflow job for this annotation

GitHub Actions / CICD-base

Only use single-quotes.
node.text.col <- vector("list", nrow(tree));

Check warning on line 501 in R/add.text.R

View workflow job for this annotation

GitHub Actions / CICD-base

Only use single-quotes.
node.text.fontface <- vector("list", nrow(tree));

Check warning on line 502 in R/add.text.R

View workflow job for this annotation

GitHub Actions / CICD-base

Only use single-quotes.

# Loop to assign text to nodes
for (i in seq_len(nrow(node.text))) {
text.row <- node.text[i, ];
pos <- which(tree$tip == text.row$node);
text.value <- text.row$name;

# Check if the text contains an underscore and parse if necessary
if (length(grep('_', text.value)) > 0) {
text.split <- strsplit(text.value, split = '_')[[1]];
node.text.value <- text.split[1];
amp <- text.split[2];
call <- paste0(node.text.value, '^\'A', amp, '\'');
text.value <- parse(text = call);
}

# Assign text value, color, and font face
node.list[[pos]] <- c(node.list[[pos]], text.value);
node.text.col[[pos]] <- c(node.text.col[[pos]], if (!is.na(text.row$col)) text.row$col else 'black');
node.text.fontface[[pos]] <- c(node.text.fontface[[pos]], if (!is.na(text.row$fontface)) text.row$fontface else 'plain');
}

tree.max.adjusted <- apply(
tree,
Expand Down Expand Up @@ -533,15 +555,15 @@ add.text2 <- function(
);
tree.max.adjusted$tipx <- (
tree.max.adjusted$tipx -
-angle.modifier * node.radius * sin(tree.max.adjusted$angle)
angle.modifier * node.radius * sin(tree.max.adjusted$angle)
);
tree.max.adjusted$basey <- (
tree.max.adjusted$basey +
angle.modifier * node.radius * cos(tree.max.adjusted$angle)
);
tree.max.adjusted$tipy <- (
tree.max.adjusted$tipy -
-angle.modifier * node.radius * cos(tree.max.adjusted$angle)
angle.modifier * node.radius * cos(tree.max.adjusted$angle)
);

# Push a viewport the same size as the final panel
Expand All @@ -552,7 +574,7 @@ add.text2 <- function(
pushViewport(viewport(
height = unit(panel.height, 'inches'),
name = 'ref',
width = unit(panel.width,'inches'),
width = unit(panel.width, 'inches'),
xscale = xlims,
yscale = c(ymax, -2)
));
Expand All @@ -563,8 +585,8 @@ add.text2 <- function(
tree.max.adjusted$y0 <- convertY(unit(tree.max.adjusted$basey, 'native'), 'inches', valueOnly = TRUE);
tree.max.adjusted$y1 <- convertY(unit(tree.max.adjusted$tipy, 'native'), 'inches', valueOnly = TRUE);

tree.max.adjusted$y <- convertY(unit(tree.max$tipy, 'native'), 'inches', valueOnly = TRUE); # Actual node positions
tree.max.adjusted$x <- convertX(unit(tree.max$tipx, 'native'), 'inches', valueOnly = TRUE);
tree.max.adjusted$y <- convertY(unit(tree.max.adjusted$tipy, 'native'), 'inches', valueOnly = TRUE); # Actual node positions
tree.max.adjusted$x <- convertX(unit(tree.max.adjusted$tipx, 'native'), 'inches', valueOnly = TRUE);

tree.max.adjusted$slope <- (tree.max.adjusted$y1 - tree.max.adjusted$y0) / (tree.max.adjusted$x1 - tree.max.adjusted$x0);
tree.max.adjusted$intercept <- tree.max.adjusted$y1 - tree.max.adjusted$slope * tree.max.adjusted$x1;
Expand Down Expand Up @@ -597,7 +619,6 @@ add.text2 <- function(
children = text.grob.gList,
vp = make.plot.viewport(clone.out, clip = 'off')
);

return(text.tree);
}

Expand All @@ -615,4 +636,4 @@ add.text2 <- function(
);

return(list(text.tree, tree.max.adjusted));
}
}

0 comments on commit b0b6440

Please sign in to comment.