ggobi/ggally

Enable negative or logical index in columns= argument to ggpairs().

krivit opened this issue · 0 comments

Minimal example

library(GGally)
data(iris)

ggpairs(iris)

# Colour-code by Species:
ggpairs(iris, aes(col=Species, fill=Species, alpha=I(0.3))) # Species column and row redundant.

ggpairs(iris, aes(col=Species, fill=Species, alpha=I(0.3)), columns=1:4, legend=1) # Works

ggpairs(iris, aes(col=Species, fill=Species, alpha=I(0.3)), columns=-5, legend=1) # Error
#> Error in fix_column_values(data, columns, columnLabels, "columns", "columnLabels"): Make sure your numeric 'columns' values are positive.
#>  columns = c(-5)

Discussion

Since negative and logical indices are standard in R, it would be helpful if one could use them to specify columns to exclude. This could be fixed by mapping it through a vector, e.g., columns <- seq_len(ncol(data))[columns], around line 99 below:

ggally/R/ggpairs.R

Lines 85 to 112 in 53c1f65

if (is.character(columns)) {
colNumValues <- lapply(columns, function(colName){
which(colnamesData == colName)
})
isFound <- as.logical(unlist(lapply(colNumValues, length)))
if (any(!isFound)) {
stop(
"Columns in '", columnsName, "' not found in data: c(",
str_c(str_c("'", columns[!isFound], "'"), collapse = ", "),
"). Choices: c('", paste(colnamesData, collapse = "', '"), "')"
)
}
columns <- unlist(colNumValues)
}
if (any(columns > ncol(data))) {
stop(
"Make sure your numeric '", columnsName, "'",
" values are less than or equal to ", ncol(data), ".\n",
"\t", columnsName, " = c(", str_c(columns, collapse = ", "), ")"
)
}
if (any(columns < 1)) {
stop(
"Make sure your numeric '", columnsName, "' values are positive.", "\n",
"\t", columnsName, " = c(", paste(columns, collapse = ", "), ")"
)
}

I would be happy to submit a PR.