Skip to content

Commit

Permalink
fix plotLoadings error caused by long variable names. Closes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
aljabadi committed Oct 25, 2019
1 parent 425011b commit 2901a9c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
4 changes: 3 additions & 1 deletion R/plotLoadings.mint.spls.R
Expand Up @@ -173,14 +173,16 @@ xlim = NULL,

#display barplot with names of variables
#added condition if all we need is the contribution stats
colnames.X <- .trim_long_names(colnames.X) ## issue 45

if (!is.null(title) & length(block) > 1)
{
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/2), 6, 2))
} else {
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/2), 4, 2))
}

mp = barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
cex.names = size.name, cex.axis = 0.7, beside = TRUE, border = border, xlim = xlim)

if ( length(block) == 1 & is.null(title) )
Expand Down
4 changes: 3 additions & 1 deletion R/plotLoadings.mint.splsda.R
Expand Up @@ -230,14 +230,16 @@ xlim = NULL,

#display barplot with names of variables
#added condition if all we need is the contribution stats
colnames.X <- .trim_long_names(colnames.X) ## issue 45

if (!is.null(title) & length(block) > 1)
{
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/2), 6, 2))
} else {
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/2), 4, 2))
}

mp = barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
cex.names = size.name, cex.axis = 0.7, beside = TRUE, border = border, xlim = xlim[i, ])

if ( length(block) == 1 & is.null(title) )
Expand Down
3 changes: 2 additions & 1 deletion R/plotLoadings.pca.R
Expand Up @@ -72,9 +72,10 @@ xlim = NULL,
df = data.frame(importance = value.selected.var) # contribution of the loading

# barplot with contributions
colnames.X <- .trim_long_names(colnames.X) ## issue 45
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/3), 4, 2))

mp = barplot(df$importance, horiz = TRUE, las = 1, col = col, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
barplot(df$importance, horiz = TRUE, las = 1, col = col, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
cex.names = size.name, cex.axis = 0.7, beside = TRUE, border = border, xlim = xlim)

if (is.null(title))
Expand Down
4 changes: 3 additions & 1 deletion R/plotLoadings.spls.R
Expand Up @@ -83,14 +83,16 @@ xlim = NULL,
df = data.frame(importance = value.selected.var) # contribution of the loading

# barplot with contributions
colnames.X <- .trim_long_names(colnames.X) ## issue 45

if (!is.null(title) & length(block) > 1)
{
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/3), 6, 2))
} else {
par(mar = c(4, max(7, max(sapply(colnames.X, nchar),na.rm = TRUE)/3), 4, 2))
}

mp = barplot(df$importance, horiz = TRUE, las = 1, col = col, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
barplot(df$importance, horiz = TRUE, las = 1, col = col, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
cex.names = size.name, cex.axis = 0.7, beside = TRUE, border = border, xlim = xlim[i,])

if ( (length(block) == 1 & is.null(title)) | (length(block) > 1 & missing(subtitle)))
Expand Down
3 changes: 2 additions & 1 deletion R/plotLoadings.splsda.R
Expand Up @@ -154,6 +154,7 @@ xlim = NULL,
}

# display barplot with names of variables
colnames.X <- .trim_long_names(colnames.X) ## issue 45
if (plot) # condition if all we need is the contribution stats
{
if (!is.null(title) & length(block) > 1)
Expand All @@ -163,7 +164,7 @@ xlim = NULL,
par(mar = c(4, max(7, max(sapply(colnames.X, nchar), na.rm = TRUE)/3), 4, 2))
}

mp = barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
barplot(df$importance, horiz = TRUE, las = 1, col = df$color, axisnames = TRUE, names.arg = colnames.X, #names.arg = row.names(df),
cex.names = size.name, cex.axis = 0.7, beside = TRUE, border = border, xlim = xlim[i, ])

if ( length(block) == 1 & is.null(title) )
Expand Down
29 changes: 29 additions & 0 deletions R/utils.R
@@ -0,0 +1,29 @@
## ----------- .trim_long_names -----------
#' Trim long variable names
#'
#' Trims feature names longer than \code{len} adding \code{...} at the end of
#' the name with a message. This helps avoid margin errors in plots.
#' \code{len=23} is chosen as default only because longest ensemble id for
#' mouse/human genes/transcripts is this length.
#'
#' @param var_names character vector of variable names
#' @param len integer, names longer than this will be trimmed so total length
#' will be \code{len}, including \code{...}.
#'
#' @return character vector of trimmed names
#'
#' @examples
#' .trim_long_names(var_names = c("long-variable-name-of-length-31", "short-variable"))
#' @noRd
.trim_long_names <- function(var_names=c("long-variable-name-of-length-31", "short-variable"), len=23) {
if (any(nchar(var_names) > len)) {
message(sprintf("Some variable names are too long. Trimmed for visualisation purposes."))

var_names <- sapply(var_names, function(char) {
ifelse(nchar(char) > len, sprintf("%s...", substring(char, 1, len-3)) , char)
})
}

return(var_names)
}

0 comments on commit 2901a9c

Please sign in to comment.