r-gregmisc / gtools

Functions to assist in R programming
25 stars 6 forks source link

applying mixedsort on one column but plotting other column-messes the sorting #17

Closed Jigyasa3 closed 2 years ago

Jigyasa3 commented 2 years ago

Hey all,

Thank you for a great package! I was wondering if there is a way around using mixedsort on a column with a combination of alpha numeric and dates?

My dataset looks like this- 13 t2 11/30/2016 18:00 14 t3 12/4/2016 18:00 15 t1 11/12/2016 8:00 16 t2 12/4/2016 18:00 17 t1 11/12/2016 8:00 18 t2 11/30/2016 18:00 19 t3 12/4/2016 18:00 20 t1 11/9/2016 12:00 21 t2 11/30/2016 18:00 22 t3 12/4/2016 18:00 23 t1 10/12/2010 24 t2 1/17/2011 25 t3 2/19/2011 26 t4 3/11/2011 27 t5 5/3/2011 28 t1 pre_tret 29 t2 post_tret 30 t1 pre_tret 31 t2 post_tret 32 t1 pre_tret 33 t2 post_tret

I am sorting on the column timepoint, but if I plot the column collection_time_all, then the sorting is completely gone and the labels are randomly plotted.

image

image

Any suggestions on how to keep the sorting from one column to another? When I examine the sorted data frame, all the columns are sorted based on the column of interest. But why is it not conserved in the plot?

bbolker commented 2 years ago

Your example isn't quite reproducible, but I think your confusion is that ggplot plots factors in order of their factor levels, not based on the row-ordering of the data frame ...

Treatment <- c(
   "Control", "Aspirin 10mg/day", "Aspirin 50mg/day",
   "Aspirin 100mg/day", "Acetomycin 100mg/day",
   "Acetomycin 1000mg/day"
)
dd <- data.frame(Treatment, x = c(6, 3:5, 1:2))
set.seed(101)
dd <- dd[sample(nrow(dd)),] ## scramble order
library(dplyr)
library(ggplot2)
library(gtools)

gg0 <- ggplot(dd, aes(Treatment, x)) + geom_point()
print(gg0)

dd_mx <- dd[rev(mixedorder(dd$Treatment)),]

gg0 %+% dd_mx ## no difference

dd_tt <- dd |> mutate(across(Treatment, factor, levels = mixedsort(Treatment)))

gg0 %+% dd_tt

Related: https://stackoverflow.com/a/15116374/190277