knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%", message = F, warning = F, error = F )
Creating cohort tables from event data is complicated and requires several lines of code. The cohorts package lets users convert data frames to cohort tables in both long and wide formats with simple functions. Users may choose between day and month level cohorts.
You can install the released version of cohorts from CRAN with:
install.packages("cohorts")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("PeerChristensen/cohorts")
In this example, we use a dataset consisting of customer IDs and invoice dates.
library(cohorts) head(online_cohorts)
We can then turn this into a cohort table where each customer ID is tracked from the first invoice month until the last month in the period.
online_cohorts %>% cohort_table_month(CustomerID, InvoiceDate)
If we need to track activity on a daily basis, we can instead use the cohort_table_month()
function.
gamelaunch %>% cohort_table_day(userid, eventDate)
In order to see the percent of remaining customers in subsequent periods, we can pipe the above code into the cohort_table_pct()
function.
gamelaunch %>% cohort_table_day(userid, eventDate) %>% cohort_table_pct(decimals = 1)
Another option is to shift cohort tables left. Here, we align cohorts such that date columns are replaced by time periods, i.e. t0, t1, t2 etc.
To left-shift a cohort table, we can use the shift_left()
function.
gamelaunch %>% cohort_table_day(userid, eventDate) %>% shift_left()
We can also get the raw numbers as percentages.
gamelaunch %>% cohort_table_day(userid, eventDate) %>% shift_left_pct()
To visualize the data, we can turn a cohort table into long format and create a line plot.
In this example, we select only the first seven cohorts.
library(tidyverse) gamelaunch_long <- gamelaunch %>% cohort_table_day(userid, eventDate) %>% shift_left_pct() %>% pivot_longer(-cohort) %>% mutate(time = as.numeric(str_remove(name,"t"))) gamelaunch_long %>% filter(value > 0, cohort <= 7, time > 0) %>% ggplot(aes(time, value, colour = factor(cohort), group = cohort)) + geom_line(size = 1.5) + geom_point(size = 1.5) + theme_light()
Another way to plot a cohort table is by means of tiles. In this case we provide the percentages and colour the tiles accordingly.
gamelaunch_long %>% filter(time > 0, value > 0) %>% ggplot(aes(time, reorder(cohort, desc(cohort)))) + geom_raster(aes(fill = log(value))) + coord_equal(ratio = 1) + geom_text(aes(label = glue::glue("{round(value,0)}%")), size = 2, color = "snow") + scale_fill_gradient(guide = F) + theme_minimal() + theme(panel.grid = element_blank(), panel.border = element_blank()) + labs(y= "cohort")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.