strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
603 stars 91 forks source link

plot_stackfrq gives an error when legend.labels not specified under some conditions #841

Closed douglaswhitaker closed 1 year ago

douglaswhitaker commented 1 year ago

If every item has the same number of chosen responses and legend.labels is not specified, an error is generated by the code that tries to guess the legend labels.

Minimal working example:

set.seed(31)
dat1 <- data.frame(matrix(sample(1:4, size = 100, replace = TRUE), ncol = 10))
sjPlot::plot_stackfrq(dat1)

image

Minimal error example:

set.seed(32)
dat2 <- data.frame(matrix(sample(1:4, size = 100, replace = TRUE), ncol = 10))
sjPlot::plot_stackfrq(dat2)

Error:

Error in `levels<-`(`*tmp*`, value = as.character(levels)) : 
  factor level [2] is duplicated

If legend.labels is specified, there is no error:

sjPlot::plot_stackfrq(dat2, legend.labels = 1:4)

image