Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
krivit opened this issue Jun 12, 2022 · 0 comments
Open

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

krivit opened this issue Jun 12, 2022 · 0 comments

Comments

@krivit
Copy link

krivit commented Jun 12, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant