ACCLAB / dabestr

Data Analysis with Bootstrap Estimation in R
https://acclab.github.io/dabestr
Apache License 2.0
211 stars 34 forks source link

Factor levels and aesthetics in estimation plot #136

Open andrewd789 opened 1 year ago

andrewd789 commented 1 year ago

Hi,

This is a great package, but I'm having difficulty assigning colours to a factor in an estimation plot. The colours in the plot do not seem to respect the factor levels. If I change the factor level order, the colour mapping in the plot does not change accordingly. Here is a simple example:

# Make some data:
value = c(rnorm(20), (rnorm(20)+3))
f1 = c(rep("cats", 20), rep("dogs", 20))
f2 = rep(c("A","B","C","D","E"), 4)
df = data.frame(f1, f2, value)

# Set a colour palette:
pal_5 = c("#9cd345","#9646c7","#7fcf93","#cb6096","#53683c")

# Set factor levels:
df$f2 = factor(df$f2, levels = c("A","B","C","D","E"), ordered = TRUE)

# Make the estimation plot:
md = df %>% 
  dabest(f1, value,
         idx = c("cats","dogs"),
         id.column = f2, paired = TRUE) %>% mean_diff()
md %>% plot(color.column = f2, palette = pal_5, 
            slopegraph = FALSE, rawplot.type = "swarmplot", rawplot.markersize = 3)

# Check the factor levels:
md$data$f2
 [1] A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E
Levels: A < B < C < D < E

image

# Now change (reverse) the factor levels
df$f2 = factor(df$f2, levels = c("E","D","C","B","A"), ordered = TRUE)

# Make the estimation plot again:
md = df %>% 
  dabest(f1, value,
         idx = c("cats","dogs"),
         id.column = f2, paired = TRUE) %>% mean_diff()
md %>% plot(color.column = f2, palette = pal_5, 
            slopegraph = FALSE, rawplot.type = "swarmplot", rawplot.markersize = 3)

# Exactly the same plot as above, despite the factor levels being different:
md$data$f2
 [1] A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E A B C D E
Levels: E < D < C < B < A

I would expect the second plot to have reversed order of levels in the legend. i.e. E would be first and pale green, D would be second and purple, etc. Why doesn't this happen?

Thanks!

Andrew.

ZHANGROU-99 commented 1 year ago

Hi, @andrewd789

Thanks for bringing this up. Yes, the curret color mapping doesn't follow the factor level order. You can change the color palette order first. We will improve the color mapping in future version.

sunroofgod commented 6 months ago

Hi @andrewd789,

We've recently released a new version of dabestr, 
“Lapis” (v2023.9.12), which adds estimation graphics for four new data types: repeated measures, 2 × 2 designs, proportions, and easy meta-analyses.

dabestr version “Lapis” is a complete rebuild of the original dabestr and as such, unfortunately, v0.3.0 of dabestr and below are now deprecated and will no longer be receiving support.

We strongly recommend upgrading to the latest version (“Lapis”) as soon as possible. This will not only ensure access to the latest features but also expedite the resolution of any issues you may have encountered.

You can find the latest release here and the updated documentation for the package here.

For quick installation, you may refer to the following:

# Install it from CRAN
install.packages("dabestr")

# Or the development version from GitHub:
# install.packages("devtools")
devtools::install_github(repo = "ACCLAB/dabestr", ref = "dev")

If you face any challenges during the update process or have further questions, please don't hesitate to reach out.

We value your feedback and appreciate your continued support!