You may like to reorder the categories in a variable so that the order categories is in line with the absolute counts.
For example, in the below, you want to reorder Purchase likelihood (on the left) so that it is in descending order of values by default (like the right). In doing this, you want to anchor "DK" to the bottom as well.
This can be accomplished with simple R code. Using the above example (which is from the Brand Health sample dataset, please contact us if you need a copy of this dataset). It assumes you know already How to log in and access your dataset with R.
First, specify variable alias and category names that should always go last.
variable <- "purchase"
end_categories <- c("DK")
Note: if you want multiple categories anchored to the bottom, you need to specify them (eg: c("DK", "Other")).
And then run the R code below.
tabs <- as.array(crtabs(formula(paste0("~", variable)), ds))
display_order <- names(tabs)[order(-tabs)]
end_categories_exist <- intersect(end_categories, display_order)
if (length(end_categories_exist) > 0) {
display_order <- c(setdiff(display_order, end_categories_exist), end_categories_exist)
}
if (type(ds[[variable]]) == "multiple_response") {
subvariables(ds[[variable]]) <- subvariables(ds[[variable]])[display_order]
} else if (type(ds[[variable]]) == "categorical") {
missing_cats <- names(categories(ds[[variable]])[is.na(categories(ds[[variable]]))])
display_order <- c(display_order, setdiff(missing_cats, display_order))
categories(ds[[variable]]) <- categories(ds[[variable]])[display_order]
}