Hi,
I just wanted to plot some boxplots for some 50 variables or so and wasn't aware of the factor_key option in gather ;-( !
And so it happened that it messed up all the titles in my facet box-plot. Since I didn't have domain knowledge I didn't notice the error before it was (kind of) too late. (Nobody was hurt!)
I was just wondering what is the reasoning behind the factor_key option and it's default FALSE setting? When do we not want to keep the ordering in the key columns?
I've attached the example code to clarify the problem.
Would be great to hear your thoughts on this.
Thanks,
Till
This is how it looks with the default factor_key=FALSE setting:
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag(): dplyr, stats
library(ggplot2)
library(reprex)
my_selection <- c("disp", "hp", "qsec", "wt", "vs", "carb")
p <- mtcars %>%
select(mpg, cyl, my_selection) %>%
gather(key = variables, value = var_values, my_selection, factor_key = FALSE) %>%
ggplot(aes(x = as.factor(cyl), y = var_values)) +
geom_boxplot() +
geom_point() +
labs(x = "cyl") +
labs(y = "var_values") +
facet_wrap(~ variables) +
theme_bw()
plot(p)
and this is how it looks with the optional factor_key=TRUE setting.
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag(): dplyr, stats
library(ggplot2)
library(reprex)
my_selection <- c("disp", "hp", "qsec", "wt", "vs", "carb")
p <- mtcars %>%
select(mpg, cyl, my_selection) %>%
gather(key = variables, value = var_values, my_selection, factor_key = TRUE) %>%
ggplot(aes(x = as.factor(cyl), y = var_values)) +
geom_boxplot() +
geom_point() +
labs(x = "cyl") +
labs(y = "var_values") +
facet_wrap(~ variables) +
theme_bw()
plot(p)