-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
184 lines (152 loc) · 5.39 KB
/
README.Rmd
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
output:
github_document:
html_preview: false
---
<!-- rmarkdown::render("README.Rmd") -->
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = file.path("man", "figures", ""),
fig.height = 8,
fig.width = 12
)
```
# autocogs
[](https://travis-ci.org/schloerke/autocogs)
[](https://codecov.io/github/schloerke/autocogs?branch=master)
[](https://cran.r-project.org/package=autocogs)
Cognostics are univariate statistics (or metrics) for a subset of data. When paired with the underlying data of visualizations, cognostics are a powerful tool for ordering and filtering the visualizations. `add_panel_cogs()` will automatically append cognostics for each plot player in a given panel column. The newly appended data can be fed into a [trelliscopejs](https://github.com/hafen/trelliscopejs) widget for easy viewing.
## Installation
You can install autocogs from github with:
```{r gh-installation, eval = FALSE}
remotes::install_github("schloerke/autocogs")
```
## Examples
### Gapminder
```{r explore}
library(autocogs)
library(tidyverse)
library(gapminder)
# remotes::install_github("hafen/trelliscopejs")
# remotes::install_github("schloerke/trelliscopejs@autocogs")
library(trelliscopejs)
# Explore
p <-
ggplot(gapminder, aes(year, lifeExp)) +
geom_line(aes(group = country)) +
geom_smooth(method = "lm", formula = y ~ x)
p
```
Looking at the plot above, most countries follow a linear trend: As the year increases, life expectancy goes up. A few countries do not follow a linear trend.
In the examples below, we will extract cognostics to aid in exploring the countries whose life expectancy is not linear.
#### `trelliscopejs::facet_trelliscope()`
```{r facet_trelliscope}
ggplot(gapminder, aes(year, lifeExp)) +
geom_smooth(method = "lm", formula = y ~ x) +
geom_line() +
trelliscopejs::facet_trelliscope(
~ country + continent,
nrow = 3, ncol = 6,
self_contained = TRUE,
auto_cog = TRUE,
state = list(
# set the state to display the country, continent, and R^2 value
# sorted by ascending R^2 value
sort = list(trelliscopejs::sort_spec("_lm_r2")),
labels = c("country", "continent", "_lm_r2")
),
path = "readme-figs/facet"
)
# (screen shot of trelliscopejs widget)
```
#### `trelliscopejs::trelliscope()`
This is a full, start to finish example how automatic cognostics could be inserted into a data exploration workflow.
```{r gapminder}
# Find a consistent y range
y_range <- range(gapminder$lifeExp)
## # Set up data and panel column
gapminder %>%
group_by(country, continent) %>%
# nest the data according to the country and continent
nest() %>%
mutate(
# create a column of plots with a
# * line
# * linear model
panel = lapply(data, function(dt) {
ggplot(dt, aes(year, lifeExp)) +
geom_smooth(method = "lm", formula = y ~ x) +
geom_line() +
ylim(y_range[1], y_range[2])
})
) %>%
print() ->
gap_data
# Double check the plot worked...
# Look at the first panel (ggplot2 plot) of Afghanistan
gap_data$panel[[1]]
#!!!!!!!!!!
# Add cognostic information given the panel column plots
#!!!!!!!!!!
gap_data %>%
autocogs::add_panel_cogs() %>%
ungroup() %>%
# double check it was added
print(width = 100) ->
full_gap_data
# Display the panel and cognostics in a trelliscopejs widget
trelliscopejs::trelliscope(
full_gap_data, "gapminder life expectancy",
panel_col = "panel",
ncol = 6, nrow = 3,
auto_cog = FALSE,
self_contained = TRUE,
state = list(
# sort by ascending R^2 value (percent explained by linear model)
sort = list(trelliscopejs::sort_spec("_lm_r2")),
# display the country, continent, and R^2 value
labels = c("country", "continent", "_lm_r2")
),
path = "readme-figs/manually"
)
# (screen shot of trelliscopejs widget)
```
### Custom Cognostics
* `add_cog_group()` to add a custom cognostics group.
* `add_layer_cogs()` to call which cognostics groups should be executed for a given plot layer.
Using existing code from the `autocogs` package, we will add the univariate continuous cognostics group.
```r
add_cog_group(
"univariate_continuous",
field_info("x", "continuous"),
"univariate metrics for continuous data",
function(x, ...) {
x_range <- range(x, na.rm = TRUE)
list(
min = cog_desc(x_range[1], "minimum of non NA data"),
max = cog_desc(x_range[2], "maximum of non NA data"),
mean = cog_desc(mean(x, na.rm = TRUE), "mean of non NA data"),
median = cog_desc(median(x, na.rm = TRUE), "median of non NA data"),
var = cog_desc(var(x, na.rm = TRUE), "variance of non NA data")
)
}
)
```
We can then call the `'univariate_continuous'` cognostics group whenever a `geom_rug` layer is added in a ggplot2 plot object using the code below.
```r
add_layer_cogs(
# load_all(); p <- qplot(x = 1, y = Sepal.Length, data = iris, geom = "boxplot"); plot_cogs(p)
"geom_boxplot",
"boxplot plot",
cog_group_df(
"univariate_continuous", "y", "_y",
"boxplot", "y", "_boxplot",
"univariate_counts", "y", "_n"
)
)
```
<!-- ## Connecting other plot objects
There are -->