diff --git a/R/plotLoadings.mint.spls.R b/R/plotLoadings.mint.spls.R index 5a597864..8617b0b6 100644 --- a/R/plotLoadings.mint.spls.R +++ b/R/plotLoadings.mint.spls.R @@ -173,6 +173,8 @@ 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)) @@ -180,7 +182,7 @@ xlim = NULL, 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) ) diff --git a/R/plotLoadings.mint.splsda.R b/R/plotLoadings.mint.splsda.R index b54daaa5..1d311598 100644 --- a/R/plotLoadings.mint.splsda.R +++ b/R/plotLoadings.mint.splsda.R @@ -230,6 +230,8 @@ 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)) @@ -237,7 +239,7 @@ xlim = NULL, 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) ) diff --git a/R/plotLoadings.pca.R b/R/plotLoadings.pca.R index cef737a2..1b3cfe30 100644 --- a/R/plotLoadings.pca.R +++ b/R/plotLoadings.pca.R @@ -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)) diff --git a/R/plotLoadings.spls.R b/R/plotLoadings.spls.R index ab400827..76e5ea50 100644 --- a/R/plotLoadings.spls.R +++ b/R/plotLoadings.spls.R @@ -83,6 +83,8 @@ 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)) @@ -90,7 +92,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 = 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))) diff --git a/R/plotLoadings.splsda.R b/R/plotLoadings.splsda.R index 78e79c47..72e21679 100644 --- a/R/plotLoadings.splsda.R +++ b/R/plotLoadings.splsda.R @@ -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) @@ -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) ) diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 00000000..18b950fb --- /dev/null +++ b/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) +} +