strengejacke/sjPlot

plot_scatter: Group labels sometimes reversed when grouping by character vectors

igelstorm opened this issue · 0 comments

Description

See example images and reprex below. When a character vector is passed as the grp argument to plot_scatter(), the groups are sometimes incorrectly labelled in the plot legend. Whether this happens or not seems to depend on alphabetical ordering. Plot A and B below differ by which of the two levels comes first in the alphabet, and in plot A, the labels are the wrong way around compared to the plots B and C (where C uses the original numeric variable).

Expected behaviour

Plots A and B should show the same thing (i.e. manual transmission cars clustered in the top left). Or at least it should be stated in the documentation that character vectors don't work reliable.

Code

test_data <- mtcars
test_data$am_string1 <- ifelse(test_data$am == 1, "Manual", "Automatic")
test_data$am_string2 <- ifelse(test_data$am == 1, "Manual", "Not manual")

sjPlot::plot_scatter(test_data, wt, mpg, am_string1, "1: Character (incorrect)")
sjPlot::plot_scatter(test_data, wt, mpg, am_string2, "2: Character (correct)")
sjPlot::plot_scatter(test_data, wt, mpg, am, "3: Numeric (0=automatic, 1=manual)")

Images

plot_a
plot_b
plot_c