Created
June 11, 2017 00:51
-
-
Save bbest/d6cb72e2a000fc50bf7e10196000ed5a to your computer and use it in GitHub Desktop.
demonstration of Tibble -> Nest -> Model -> Plot -> TrelliscopeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Tibble -> Nest -> Model -> Plot -> TrelliscopeJS | |
Source: [schloerke/stat695data](https://github.com/schloerke/stat695data#gapminder) | |
```{r} | |
library(gapminder) | |
tibble::glimpse(gapminder) | |
# look per continent | |
qplot(year, lifeExp, data = gapminder, geom = "line", group = country) + | |
facet_wrap(~ continent) | |
# split to each country | |
qplot(year, lifeExp, data = gapminder, geom = "line", group = country) + | |
facet_trelliscope(~ country, nrow = 2, ncol = 4, path = "_gggapminder") | |
country_model <- function(df) { | |
lm(lifeExp ~ year, data = df) | |
} | |
# look at each country individually | |
gapminder %>% | |
group_by(country, continent) %>% | |
nest() %>% | |
mutate( | |
model = purrr::map(data, country_model) | |
) %>% | |
mutate( | |
model_info = purrr::map(model, broom::glance) | |
) %>% | |
unnest(model_info) %>% | |
select(country, continent, data, r.squared) %>% | |
mutate( | |
panel = map_plot(data, | |
~ figure(ylim = c(10, 95), toolbar = NULL) %>% | |
ly_lines(year, lifeExp, data = .x) %>% | |
ly_points(year, lifeExp, hover = .x, data = .x) %>% | |
theme_axis("x", major_label_orientation = 45) | |
) | |
) %>% | |
trelliscope(name = "rbokeh_gapminder", nrow = 2, ncol = 4) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment