Skip to content

Commit

Permalink
add repel arg to plotIndiv and plotVar. re mixOmicsTeam#129
Browse files Browse the repository at this point in the history
  • Loading branch information
aljabadi committed Jun 1, 2021
1 parent f1671f8 commit efa7dba
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Suggests: BiocStyle,
knitr,
rmarkdown,
testthat,
ggrepel,
rgl
Authors@R:
c(person("Kim-Anh", "Le Cao", role = c("aut", "cre"), email = "[email protected]"),
Expand Down
11 changes: 7 additions & 4 deletions R/internal_graphicModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal_graphicModule <-
zlim,
class.object,
display.names,
repel = FALSE,
legend,
abline,
star,
Expand Down Expand Up @@ -258,11 +259,13 @@ internal_graphicModule <-
{
if (display.names)
{
p = p +geom_point(data = subset(df, col == i &
df$Block == levels(df$Block)[j]),size = 0, shape = 0,
.geom_text <- .get_geom_text(repel = repel, style = style)

p = p + geom_point(data = subset(df, col == i &
df$Block == levels(df$Block)[j]),size = ifelse(repel, 2, 0),
color = unique(df[df$col == i & df$Block ==
levels(df$Block)[j], ]$col))+
geom_text(data = subset(df, col == i & df$Block ==
levels(df$Block)[j], ]$col), shape = 16)+
.geom_text(data = subset(df, col == i & df$Block ==
levels(df$Block)[j]),
aes(label = names),
color = df[df$col == i & df$Block ==
Expand Down
3 changes: 3 additions & 0 deletions R/plotIndiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#' @param ind.names either a character vector of names for the individuals to
#' be plotted, or \code{FALSE} for no names. If \code{TRUE}, the row names of
#' the first (or second) data matrix is used as names (see Details).
#' @template arg/repel
#' @param group factor indicating the group membership for each sample, useful
#' for ellipse plots. Coded as default for the supervised methods \code{PLS-DA,
#' SPLS-DA,sGCCDA}, but needs to be input for the unsupervised methods
Expand Down Expand Up @@ -160,6 +161,7 @@ check.input.plotIndiv <-
blocks = NULL,
# to choose which block data to plot, when using GCCA module
ind.names = TRUE,
repel = repel,
style = "ggplot2",
# can choose between graphics, 3d, lattice or ggplot2
#study = "global",
Expand Down Expand Up @@ -192,6 +194,7 @@ check.input.plotIndiv <-
if (!style %in% c("ggplot2", "lattice", "graphics", "3d"))
stop("'style' must be one of 'ggplot2', 'lattice', 'graphics' or '3d' .", call. = FALSE)


#-- axes.box
choices = c("box", "bbox", "both")
axes.box = choices[pmatch(axes.box, choices)]
Expand Down
5 changes: 3 additions & 2 deletions R/plotIndiv.pls.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plotIndiv.mixo_pls <-
comp = NULL,
rep.space = NULL,
ind.names = TRUE,
repel = FALSE,
group,
col.per.group,
style = "ggplot2",
Expand Down Expand Up @@ -103,7 +104,7 @@ plotIndiv.mixo_pls <-
stop("'background' must have been obtained with the 'background.predict' function")

#-- check inputs
check = check.input.plotIndiv(object = object, comp = comp , blocks = blocks, ind.names = ind.names,
check = check.input.plotIndiv(object = object, comp = comp , blocks = blocks, ind.names = ind.names, repel = repel,
style = style, ellipse = ellipse, ellipse.level = ellipse.level, centroid = centroid,
star = star, legend = legend, X.label = X.label, Y.label = Y.label, Z.label = Z.label, abline = abline,
xlim = xlim, ylim = ylim, alpha = alpha, axes.box = axes.box, plot_parameters = plot_parameters)
Expand Down Expand Up @@ -167,7 +168,7 @@ plotIndiv.mixo_pls <-
#call plot module (ggplot2, lattice, graphics, 3d)
res = internal_graphicModule(df = df, centroid = centroid, col.per.group = col.per.group, title = title,
X.label = X.label, Y.label = Y.label, Z.label = Z.label, xlim = xlim, ylim = ylim, class.object = class(object),
display.names = display.names, legend = legend, abline = abline, star = star,
display.names = display.names, repel = repel, legend = legend, abline = abline, star = star,
ellipse = ellipse, df.ellipse = df.ellipse, style = style, layout = layout, #missing.col = missing.col,
axes.box = axes.box, plot_parameters = plot_parameters, alpha = alpha, background = background)

Expand Down
13 changes: 12 additions & 1 deletion R/plotVar.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#' @param var.names either a character vector of names for the variables to be
#' plotted, or \code{FALSE} for no names. If \code{TRUE}, the col names of the
#' first (or second) data matrix is used as names.
#' @template arg/repel
#' @param blocks for an object of class \code{"rgcca"} or \code{"sgcca"}, a
#' numerical vector indicating the block variables to display.
#' @param X.label x axis titles.
Expand Down Expand Up @@ -109,6 +110,7 @@ plotVar <-
comp.select = comp,
plot = TRUE,
var.names = NULL,
repel = FALSE,
blocks = NULL,
# to choose which block data to plot, when using GCCA module
X.label = NULL,
Expand Down Expand Up @@ -673,14 +675,23 @@ plotVar <-
for (i in levels(df$Block))
p = p + geom_point(data = subset(df, df$Block == i), size = 0, shape = 0)

.geom_text <- .get_geom_text(repel = repel, style = style)

#-- Display sample or var.names
for (i in seq_len(length(var.names))){
if (var.names[i]) {
p = p + geom_text(data = df[c((ind.group[i] + 1) : ind.group[i + 1]), ],
p = p + .geom_text(data = df[c((ind.group[i] + 1) : ind.group[i + 1]), ],
label = df[c((ind.group[i] + 1) : ind.group[i + 1]), "names"],
size = df[c((ind.group[i] + 1) : ind.group[i + 1]), "cex"],
color = df[c((ind.group[i] + 1) : ind.group[i + 1]), "col"],
fontface = df[c((ind.group[i] + 1) : ind.group[i + 1]), "font"])

if (repel) {
p = p + geom_point(data = df[c((ind.group[i] + 1) : ind.group[i + 1]), ],
size = 2,
shape = 16,
color = df[c((ind.group[i] + 1) : ind.group[i + 1]), "col"])
}
} else {
p = p + geom_point(data = df[c((ind.group[i] + 1) : ind.group[i + 1]), ],
size = df[c((ind.group[i] + 1) : ind.group[i + 1]), "cex"],
Expand Down
34 changes: 34 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,37 @@ mixo_gg.theme <- function(cex, x.angle = 90, background.fill = 'grey97', subtitl
else
sprintf("\033[33m%s\033[39m", char)
}



#' get geom_text based on repel arg
#'
#' @param repel Logical repel arg
#' @param style Plot style. See ?plotIndiv
#' @noRd
#' @return geom_text function variation
#' @keywords Internal
.get_geom_text <- function(repel, style = 'ggplot2')
{
if (isTRUE(repel))
{

if (style != 'ggplot2')
message("repel can only be used with style = 'ggplot2'")

repel <- FALSE
if(repel && requireNamespace("ggrepel", quietly = TRUE) == FALSE)
stop("the 'ggrepel' package is required with repel=TRUE", call. = FALSE)

}

if (repel)
{
geom_text_ <- ggplot2::geom_text
} else {
out <- ggrepel::geom_text_repel
formals(out)$max.overlaps <- Inf
}
geom_text_
}

2 changes: 2 additions & 0 deletions man-roxygen/arg/repel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#' @param repel (if ind.names=TRUE), Logical indicating whether to spread the
#' names away from each other in the plot.
4 changes: 4 additions & 0 deletions man/plotIndiv.Rd

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

4 changes: 4 additions & 0 deletions man/plotVar.Rd

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

0 comments on commit efa7dba

Please sign in to comment.