ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
39 stars 11 forks source link

faceting with ordered factors #80

Closed jiyunson closed 6 years ago

jiyunson commented 6 years ago

If we have an ordered factor and label it, it shows up great in a faceted histogram:

mtcars$hp3group <- factor(ntile(mtcars$hp, 3), levels = c(1:3), labels = c("low","med","high"), ordered = TRUE)

# orders correctly
gf_histogram(~ mpg, data = mtcars) %>%
  gf_facet_grid(hp3group ~ .)

image

But then when we add group means to the faceted histogram, the levels get re-ordered alphabetically by label:

mpg.stats <- favstats(mpg ~ hp3group, data = mtcars)

# when we add group means, orders alphabetically
gf_histogram(~ mpg, data = mtcars) %>%
  gf_facet_grid(hp3group ~ .) %>%
  gf_vline(xintercept = ~mean, data = mpg.stats, color = "orange") ``

image

It would be great if it could stay ordered even when we add other elements... Thanks! Ji

rpruim commented 6 years ago

This is an issue with df_stats(), not with the plotting. df_stats() is using a character variable for hp3group.

This works just fine.

mpg.stats2 <- mtcars %>%
  group_by(hp3group) %>%
  summarise(mean = mean(mpg))

gf_histogram(~ mpg, data = mtcars, inherit = FALSE) %>%
  gf_vline(xintercept = ~mean, data = mpg.stats2, color = "orange") %>%
  gf_facet_grid(hp3group ~ .) 
rpruim commented 6 years ago

I read this too quickly. If you use df_stats() instead of favstats() things also work fine.

> str(df_stats(mpg ~ hp3group, data = mtcars))
'data.frame':   3 obs. of  10 variables:
 $ hp3group: Ord.factor w/ 3 levels "low"<"med"<"high": 1 2 3
 $ min     : num  18.1 15.2 10.4
 $ Q1      : num  22.1 18.2 13.6
 $ median  : num  24.4 19.2 14.8
 $ Q3      : num  28.9 21 15.7
 $ max     : num  33.9 30.4 17.3
 $ mean    : num  25.5 19.9 14.3
 $ sd      : num  4.99 4.04 2.32
 $ n       : int  11 11 10
 $ missing : int  0 0 0
> str(favstats(mpg ~ hp3group, data = mtcars))
'data.frame':   3 obs. of  10 variables:
 $ hp3group: chr  "low" "med" "high"
 $ min     : num  18.1 15.2 10.4
 $ Q1      : num  22.1 18.2 13.6
 $ median  : num  24.4 19.2 14.8
 $ Q3      : num  28.9 21 15.7
 $ max     : num  33.9 30.4 17.3
 $ mean    : num  25.5 19.9 14.3
 $ sd      : num  4.99 4.04 2.32
 $ n       : int  11 11 10
 $ missing : int  0 0 0