options(rmarkdown.html_vignette.check_title = FALSE) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE )
library(brolgar) library(ggplot2)
brolgar
explores two ways to explore the data, first exploring the raw data, then exploring the data using summaries. This vignette displays a variety of ways to explore your data around these two ideas.
When you first receive your data, you want to look at as much raw data as possible. This section discusses a few techniques to make it more palatable to explore your raw data without getting too much overplotting.
Sample n random individuals to explore (Note: Possibly not representative)
For example, we can sample 20 random individuals, and then plot them. (perhaps change sample_n_keys
into sample_id
.)
wages %>% sample_n_keys(size = 20) wages %>% sample_n_keys(size = 20) %>% ggplot(aes(x = xp, y = ln_wages, group = id)) + geom_line()
There was a variety of the number of observations in the data - some with only a few, and some with many. We can filter by the number of the observations in the data using add_n_obs()
, which adds a new column, n_obs
, the number of observations for each key.
wages %>% add_n_obs()
We can then filter our data based on the number of observations, and combine this with the previous steps to sample the data using sample_n_keys()
.
library(dplyr) wages %>% add_n_obs() %>% filter(n_obs >= 5) %>% sample_n_keys(size = 20) %>% ggplot(aes(x = xp, y = ln_wages, group = id)) + geom_line()
facet_strata
brolgar
provides some clever facets to help make it easier to explore your data. facet_strata()
splits the data into 12 groups by default:
set.seed(2019-07-23-1936) library(ggplot2) ggplot(wages, aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_strata()
You can control the number with n_strata
:
set.seed(2019-07-23-1936) library(ggplot2) ggplot(wages, aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_strata(n_strata = 6)
And have your regular control with other facet options:
set.seed(2019-07-23-1936) library(ggplot2) ggplot(wages, aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_strata(n_strata = 6, nrow = 3, ncol = 2)
facet_sample
facet_sample()
allows you to specify a number of samples per plot with, "n per plot" and the number of facets to show with "n facets". By default it splits the data into 12 facets with 3 per group:
set.seed(2019-07-23-1937) ggplot(wages, aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_sample()
This allows for you to look at a larger sample of the data.
We can combine add_n_obs()
and filter()
to show only series which have only 5 or more observations:
set.seed(2019-07-23-1937) wages %>% add_n_obs() %>% filter(n_obs >= 5) %>% ggplot(aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_sample()
These approaches allow you to view large sections of the raw data, but it does not point out individuals that are "interesting", in the sense of those being outliers, or representative of the middle of the group.
You can plot the features of the data by first identifying features of interest and then joining them back to the data. For a more details explanation of this, see the vignette, "Finding Features".
In this example, we will plot those whose values only increase or decrease with feat_monotonic
and gghighlight
:
library(gghighlight) wages %>% features(ln_wages, feat_monotonic) %>% left_join(wages, by = "id") %>% ggplot(aes(x = xp, y = ln_wages, group = id)) + geom_line() + gghighlight(increase)
You can explore the available features, see the function References
We can find those individuals who have a negative slope using key_slope
. For more detail on key_slope
, see the Exploratory Modelling vignette.
wages %>% key_slope(ln_wages ~ xp)
key_slope
fits a linear model for each key, and returns a tibble
with the key
columns and .intercept
and .slope_<varname>
, and any other explanatory variables.
We can use gghighlight
to identify individuals with an overall negative slope:
library(dplyr) wages_slope <- wages %>% key_slope(ln_wages ~ xp) %>% left_join(wages, by = "id") gg_wages_slope <- ggplot(wages_slope, aes(x = xp, y = ln_wages, group = id)) + geom_line() gg_wages_slope + gghighlight(.slope_xp < 0)
With a positive slope
gg_wages_slope + gghighlight(.slope_xp > 0)
We could even facet by slope:
gg_wages_slope + facet_wrap(~.slope_xp > 0)
facet_strata
We can use the along
argument of facet_strata()
to break the data according to some feature. The only catch is that the data passed must be a tsibble
.
For example, we could break the data along
the .slope_xp
variable into 12 groups, which by default will be arranged in descending order. So here we have to groups broken up from most positive slope to most negative.
wages_slope <- wages %>% key_slope(ln_wages ~ xp) %>% # ensures that we keep the data as a `tsibble` left_join(x = wages, y = ., by = "id") gg_wages_slope <- ggplot(wages_slope, aes(x = xp, y = ln_wages, group = id)) + geom_line() gg_wages_slope + facet_strata(n_strata = 12, along = .slope_xp)
We could then do this along other features of a five number summary:
wages_five <- wages %>% features(ln_wages, feat_five_num) %>% # ensures that we keep the data as a `tsibble` left_join(x = wages, y = ., by = "id") wages_five
gg_wages_five <- ggplot(wages_five, aes(x = xp, y = ln_wages, group = id)) + geom_line() gg_wages_five
We could move along the minimum:
gg_wages_five + facet_strata(n_strata = 12, along = min)
We could move along the maximum:
gg_wages_five + facet_strata(n_strata = 12, along = max)
We could move along the median:
gg_wages_five + facet_strata(n_strata = 12, along = med)
Under the hood there needs to be some summarisation of the data to arrange it like this, details on the implementation are in the helpfile for ?facet_strata
.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.