Skip to content

Unclear error for invalid xlevels param in barplot #430

@katrinabrock

Description

@katrinabrock

Problem

I wanted to reverse the order of the x axis in a bar chart. I eventually succeeded with this:

tinyplot(
  ~cyl,
  data = mtcars,
  type = "barplot",
  beside = TRUE
  ,xlevels = levels(factor(mycars$cyl))[3:1]
)

However, the helptext "a character or numeric vector specifying in which order the levels of the x variable should be plotted. " does not make it clear that that each element of the vector must be a match for one of the levels of x, not an index. I would have initially expected xlevels = 3:1 (indices) and/or xlevels = c(8, 6, 4) (actual x values passed in). These resulted in

Error in (function (datapoints, col, bg, lty, lwd, palette, xlab = NULL,  : 
  object 'x' not found

which does not indicate why my values are invalid or what valid values might be.

Suggested Remedies

  1. Fix the error message: run a check on the xlevel param to check if it matches levels of x. If not, error with something like "xlevels must match levels of x".
  2. Clarify the parameter description as to what values are acceptable and what they do.
  3. Add an example to barplots examples or gallery where xlevel is set.
  4. Accept numeric input. Either if x is numeric, allow xlevels to be values of x OR accept level indices. (Obviously doing both would create conflicts, but seems like one or the other could be done.)

Hope this is helpful!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions