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
13 changes: 2 additions & 11 deletions R/SRCGrob.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ SRCGrob <- function(
scale.size.2 = NA,
scale.padding = 1
) {
if ('CP' %in% colnames(tree) && !(plotting.direction %in% c('down', 0))) {
warning(paste(
'"plotting.direction" is not yet supported with "CP" polygon column.',
'"plotting.direction" will be ignored.'
));
}

if ('CCF' %in% colnames(tree) && !('CP' %in% colnames(tree))) {
tree$CP <- tree$CCF;
}

add.node.text <- !is.null(node.text);
add.polygons <- !is.null(tree$CP) && !disable.polygons;
Expand Down Expand Up @@ -137,7 +127,8 @@ SRCGrob <- function(
scale.bar.coords = scale.bar.coords,
scale.size.1 = scale.size.1,
scale.size.2 = scale.size.2,
scale.padding = scale.padding
scale.padding = scale.padding,
plotting.direction = plotting.direction
);

out.tree <- gTree(
Expand Down
5 changes: 2 additions & 3 deletions R/add.nodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add.node.ellipse <- function(
node.radius,
label.nodes = NULL,
label.cex = NA,
add.normal = FALSE,
scale1,
...
) {
Expand Down Expand Up @@ -101,11 +100,11 @@ add.normal <- function(clone.out, node.radius, label.cex, normal.cex = 1) {
vp = vpStack(
make.plot.viewport(clone.out, clip = 'off'),
viewport(
y = unit(1, 'npc') - unit(node.radius * normal.cex, 'inches'),
y = unit(0, 'native'),
x = unit(0, 'native'),
height = grobHeight(normal.box),
width = grobWidth(normal.box),
just = c('centre', 'bottom')
just = c('centre', 'centre')
)
)
);
Expand Down
153 changes: 104 additions & 49 deletions R/add.text.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ axis.overlap <- function(
if (return.cex & !is.null(overlaps)) {
new.cex <- cex;

while (!is.null(overlaps)) {
while (!is.null(overlaps) && new.cex > 0.05) {
new.cex <- new.cex - 0.05;
overlaps <- axis.overlap(xpos, node.text, line.dist, axis.type, new.cex, panel.width);
}
Expand Down Expand Up @@ -119,9 +119,12 @@ position.node.text <- function(
split = FALSE,
label.nodes = FALSE,
adjust.axis.overlap = TRUE,
cex = cex
cex = cex,
plotting.direction = 'down'
) {

is.horizontal <- !is.numeric(plotting.direction) && plotting.direction %in% c('left', 'right');

text.grob.list <- vector('list', length(unlist(node.list)));
orig.cex <- cex;
idx <- 1;
Expand Down Expand Up @@ -191,6 +194,10 @@ position.node.text <- function(
(!label.nodes && (label.bottom + str.heightsum) > (tree.max.adjusted$y0[s] + node.radius * 0.5))
) {
cex <- cex - 0.05;
if (cex < 0.01) {
cex <- 0.01;
break;
}
}
}

Expand All @@ -209,10 +216,9 @@ position.node.text <- function(
vjust <- 'center';
} else {
ypos <- label.bottom + (g - 1) * spacing + heights - spacing;

#back computing the x position based on the intercept and the slope
xpos <- ifelse(
is.infinite(slope),
is.infinite(slope) || is.horizontal,
yes = tree.max.adjusted$x1[s],
no = (ypos - intercept) / slope
);
Expand All @@ -235,14 +241,24 @@ position.node.text <- function(
no = sum(str.heights.left[c(1:(g - 1))])
);

ypos <- label.bottom + (g - 1) * spacing + heights - spacing;
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos - xline.dist, 'inches'),
y = unit(ypos,'inches'),
just = c('right', 'bottom'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
if (is.horizontal) {
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(ypos, 'inches'),
y = unit(xpos - xline.dist, 'inches'),
just = c('right', 'top'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
} else {
ypos <- label.bottom + (g - 1) * spacing + heights - spacing;
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos - xline.dist, 'inches'),
y = unit(ypos,'inches'),
just = c('right', 'bottom'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
}
} else {
offset.left <- ceiling(length(node.text.col[[s]]) / 2);
heights <- ifelse((
Expand All @@ -251,15 +267,24 @@ position.node.text <- function(
no = sum(str.heights.right[c(1:(g - offset.left - 1))])
);

ypos <- label.bottom + (g - offset.left - 1) * spacing + heights - spacing;

text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist, 'inches'),
y = unit(ypos, 'inches'),
just = c('left', 'bottom'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
if (is.horizontal) {
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos, 'inches'),
y = unit(ypos + xline.dist, 'inches'),
just = c('right', 'bottom'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
} else {
ypos <- label.bottom + (g - offset.left - 1) * spacing + heights - spacing;
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist, 'inches'),
y = unit(ypos, 'inches'),
just = c('left', 'bottom'),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
}
}
} else if (alternating) {
# Alternate between placing the text to the left and to the right of the node
Expand All @@ -271,15 +296,25 @@ position.node.text <- function(
xline.dist.adj <- xline.dist;
}

text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist.adj, 'inches'),
y = unit(ypos, 'inches'),
just = just,
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
if (is.horizontal) {
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos, 'inches'),
y = unit(ypos + xline.dist.adj, 'inches'),
just = c('center', ifelse(xline.dist.adj > 0, 'bottom', 'top')),
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
} else {
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist.adj, 'inches'),
y = unit(ypos, 'inches'),
just = just,
gp = gpar(col = node.text.col[[s]][g], cex = cex)
);
}

if (adjust.axis.overlap) {
if (adjust.axis.overlap && !is.horizontal) {
overlaps.axis <- axis.overlap(
xpos, node.list[[s]][g],
xline.dist.adj,
Expand All @@ -304,7 +339,8 @@ position.node.text <- function(
node.radius = node.radius,
alternating = alternating,
split = split,
label.nodes = label.nodes
label.nodes = label.nodes,
plotting.direction = plotting.direction
);

return(text.grob.list);
Expand Down Expand Up @@ -418,7 +454,7 @@ position.node.text <- function(
}
}

if (adjust.axis.overlap) {
if (adjust.axis.overlap && !is.horizontal) {
overlaps.axis <- axis.overlap(
xpos,
node.list[[s]][g],
Expand All @@ -428,8 +464,10 @@ position.node.text <- function(
return.cex = TRUE
);

# Shrink the text if they overlap
if (!is.null(overlaps.axis)) {
# Shrink the text if they overlap; only recurse when
# axis.overlap found a strictly smaller cex, otherwise
# we would loop infinitely at the minimum cex value.
if (!is.null(overlaps.axis) && overlaps.axis < cex) {
text.grob.list <- position.node.text(
tree.max.adjusted = tree.max.adjusted,
node.list = node.list,
Expand All @@ -444,25 +482,42 @@ position.node.text <- function(
node.radius = node.radius,
alternating = alternating,
split = split,
label.nodes = label.nodes
label.nodes = label.nodes,
plotting.direction = plotting.direction
);

return(text.grob.list);
}
}
}

text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist, 'inches'),
y = unit(ypos, 'inches'),
just = c(hjust, vjust),
gp = gpar(
col = node.text.col[[s]][g],
fontface = node.text.fontface[[s]][g],
cex = cex
)
);
if (is.horizontal) {
offset.x <- ceiling(length(node.text.col[[s]]) / 2);
xpos <- tree.max.adjusted$x1[s] - ((tree.max.adjusted$x1[s] - tree.max.adjusted$x0[s]) * 0.5);
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos, 'inches'),
y = unit(ypos + xline.dist, 'inches'),
just = c('center', ifelse(xline.dist > 0, 'bottom', 'top')),
gp = gpar(
col = node.text.col[[s]][g],
fontface = node.text.fontface[[s]][g],
cex = cex
)
);
} else {
text.grob.list[[idx]] <- textGrob(
node.list[[s]][g],
x = unit(xpos + xline.dist, 'inches'),
y = unit(ypos, 'inches'),
just = c(hjust, vjust),
gp = gpar(
col = node.text.col[[s]][g],
fontface = node.text.fontface[[s]][g],
cex = cex
)
);
}
}

idx <- idx + 1;
Expand Down Expand Up @@ -490,13 +545,13 @@ add.text2 <- function(
node.radius = NULL,
alternating = TRUE,
split = TRUE,
clone.out = NULL
clone.out = NULL,
plotting.direction = 'down'
) {

# Radius in native units
node.radius <- node.radius / scale;
node.text <- node.text[node.text$node %in% tree$tip, ];

node.list <- vector('list', nrow(tree));
node.text.col <- vector('list', nrow(tree));
node.text.fontface <- vector('list', nrow(tree));
Expand Down Expand Up @@ -591,7 +646,6 @@ add.text2 <- function(

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;

text.grob.list <- position.node.text(
tree.max.adjusted = tree.max.adjusted,
node.list = node.list,
Expand All @@ -606,7 +660,8 @@ add.text2 <- function(
node.radius = node.radius,
alternating = alternating,
split = split,
label.nodes = label.nodes
label.nodes = label.nodes,
plotting.direction = plotting.direction
);

text.grob.gList <- do.call(gList, text.grob.list);
Expand Down
2 changes: 1 addition & 1 deletion R/calculate.clone.polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ make.polygon <- function(
vaf <- wid;

beta <- len / beta.in;
y1 <- max(y0 + beta, y0 + 1);
y1 <- y0 + beta #max(y0 + beta, y0 + 1);
yy <- seq(y0, y1, length.out = 100);

params.d <- c(-0.7310133, sig.shape);
Expand Down
39 changes: 36 additions & 3 deletions R/make.clone.tree.grobs.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ make.clone.tree.grobs <- function(
scale.size.1,
scale.size.2,
scale.padding,
plotting.direction = 'down',
...
) {
#initializing dataframe for subclones
Expand Down Expand Up @@ -110,12 +111,42 @@ make.clone.tree.grobs <- function(
);

clone.out$no.ccf <- no.ccf;

# Rotate node positions and branch angles for fish plots (with CP data) when start.angle is non-zero
# This ensures branches and nodes rotate along with the polygons
if (!no.ccf && start.angle != 0) {
rotated.nodes <- rotate.coords(
x = clone.out$v$x,
y = clone.out$v$y,
rotate.by = start.angle,
x.origin = 0,
y.origin = 0
);
clone.out$v$x <- rotated.nodes$x;
clone.out$v$y <- rotated.nodes$y;

clone.out$tree$angle <- clone.out$tree$angle + start.angle;

for (j in seq_along(clone.out$clones)) {
rot.poly <- rotate.coords(
x = clone.out$clones[[j]]$x,
y = clone.out$clones[[j]]$y,
rotate.by = start.angle,
x.origin = 0,
y.origin = 0
);
clone.out$clones[[j]]$x <- rot.poly$x;
clone.out$clones[[j]]$y <- rot.poly$y;
}
}

plot.size <- calculate.main.plot.size(
clone.out,
scale1,
wid,
min.width,
node.radius
node.radius,
start.angle = start.angle
);

if (!no.ccf) {
Expand Down Expand Up @@ -159,7 +190,8 @@ make.clone.tree.grobs <- function(
no.ccf = no.ccf,
xaxis.label = xaxis.label,
yaxis1.label = yaxis1.label,
yaxis2.label = yaxis2.label
yaxis2.label = yaxis2.label,
plotting.direction = plotting.direction
);

if (scale.bar) {
Expand Down Expand Up @@ -200,7 +232,8 @@ make.clone.tree.grobs <- function(
node.radius = node.radius,
scale = scale1,
clone.out = clone.out,
alternating = FALSE
alternating = FALSE,
plotting.direction = plotting.direction
);

clone.out$grobs <- c(clone.out$grobs, list(node.text.grobs));
Expand Down
Loading
Loading