Closed jiyunson closed 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 ~ .)
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
If we have an ordered factor and label it, it shows up great in a faceted histogram:
But then when we add group means to the faceted histogram, the levels get re-ordered alphabetically by label:
It would be great if it could stay ordered even when we add other elements... Thanks! Ji