Description Usage Arguments Value Examples
Useful when plotting with multiple data sources.Often summary data.frames can
lose the original data levels during manipulation and when you return to plot
them facet, axis order is controlled by as.factor()
's sort order if the levels
in the objects don't match. As of forcats_v0.4.0 there is function forcats::fct_match()
,
so the assayr2 function has been renamed to fct_sync()
.
1 2 3 |
datf |
The data.frame needing to be factor leveled. |
ref_datf |
The reference data.frame with the ideal factor levels. |
A tibble.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)
data(mtcars)
mt1 <- mtcars %>% mutate(cyl = factor(cyl, levels = c(6,8,4)),
am = factor(am, levels = c(1,0)))
mt2 <- mtcars %>% split(list(.$cyl, .$am)) %>%
map_df(~ lm(mpg ~ hp, data = .) %>% coef() %>% bind_rows() %>%
set_names(c("b", "m")), .id = "splitt") %>%
separate(splitt, c("cyl", "am"), sep = "\\.")
mt1$cyl
mt2$cyl
mt1$am
mt2$am
# panel order is controlled by as.factor()
ggplot(mt1, aes(x = hp, y = mpg)) +
geom_point() +
geom_abline(data = mt2, aes(slope = m, intercept = b)) +
facet_grid(am~cyl)
# fix the panel order
mt2 <- fct_sync(mt2, mt1)
ggplot(mt1, aes(x = hp, y = mpg)) +
geom_point() +
geom_abline(data = mt2, aes(slope = m, intercept = b)) +
facet_grid(am~cyl)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.