-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
240 lines (199 loc) · 7.9 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
```{r setup,include=FALSE}
# set the knitr options ... for everyone!
# if you unset this, then vignette build bonks. oh, joy.
#opts_knit$set(progress=TRUE)
opts_knit$set(eval.after='fig.cap')
# for a package vignette, you do want to echo.
# opts_chunk$set(echo=FALSE,warning=FALSE,message=FALSE)
opts_chunk$set(warning=FALSE,message=FALSE)
#opts_chunk$set(results="asis")
opts_chunk$set(cache=TRUE,cache.path="cache/")
#opts_chunk$set(fig.path="figure/",dev=c("pdf","cairo_ps"))
opts_chunk$set(fig.path="man/figures/",dev=c("png"))
opts_chunk$set(fig.width=7,fig.height=6,dpi=100,out.width='700px',out.height='600px')
# doing this means that png files are made of figures;
# the savings is small, and it looks like shit:
#opts_chunk$set(fig.path="figure/",dev=c("png","pdf","cairo_ps"))
#opts_chunk$set(fig.width=4,fig.height=4)
# for figures? this is sweave-specific?
#opts_knit$set(eps=TRUE)
# this would be for figures:
#opts_chunk$set(out.width='.8\\textwidth')
# for text wrapping:
options(width=124,digits=2)
opts_chunk$set(size="small")
opts_chunk$set(tidy=FALSE,tidy.opts=list(width.cutoff=50,keep.blank.line=TRUE))
library(ggplot2)
library(ggallin)
library(dplyr)
library(moments)
library(microbenchmark)
# chicken and egg dept:
# [![CRAN](http://www.r-pkg.org/badges/version/ggallin)](http://cran.rstudio.com/package=ggallin)
# [![Downloads](http://cranlogs.r-pkg.org/badges/ggallin?color=brightgreen)](http://www.r-pkg.org/pkg/ggallin)
# [![Total](http://cranlogs.r-pkg.org/badges/grand-total/ggallin?color=brightgreen)](http://www.r-pkg.org/pkg/ggallin)
```
# ggallin
[![Build Status](https://github.com/shabbychef/ggallin/workflows/R-CMD-check/badge.svg)](https://github.com/shabbychef/ggallin/actions)
[![codecov.io](http://codecov.io/github/shabbychef/ggallin/coverage.svg?branch=master)](http://codecov.io/github/shabbychef/ggallin?branch=master)
[![CRAN](http://www.r-pkg.org/badges/version/ggallin)](https://cran.r-project.org/package=ggallin)
[![Downloads](http://cranlogs.r-pkg.org/badges/ggallin?color=green)](http://www.r-pkg.org/pkg/ggallin)
[![Total](http://cranlogs.r-pkg.org/badges/grand-total/ggallin?color=green)](http://www.r-pkg.org/pkg/ggallin)
> *If you think I'm into this for the money you're dead wrong because I'm not doing this for the money. I'm doing it because it lives inside of me.* -- GG Allin
A grab bag of _ggplot2_ extensions and hacks.
-- Steven E. Pav, shabbychef@gmail.com
## Installation
This package can be installed
from CRAN (not yet),
via [drat](https://github.com/eddelbuettel/drat "drat"), or
from github:
```{r install,eval=FALSE,echo=TRUE}
# via CRAN: (not yet)
# install.packages("ggallin")
# via drat:
if (require(drat)) {
drat:::add("shabbychef")
install.packages("ggallin")
}
# get snapshot from github (may be buggy)
if (require(devtools)) {
install_github('shabbychef/ggallin')
}
```
## `geom_cloud`
This `geom` acts nearly as a drop-in replacement for `geom_errorbar`,
converting `ymin` and `ymax` into 'clouds' of uncertainty with alpha
proportional to normal density.
```{r geomcloud,cache=TRUE,eval=TRUE,echo=TRUE,dpi=200,out.width='600px',out.height='500px'}
library(ggplot2)
library(ggallin)
library(dplyr)
nobs <- 1000
set.seed(2134)
mydat <- data.frame(grp=sample(c(0,1),nobs,replace=TRUE),
colfac=sample(letters[1:2],nobs,replace=TRUE),
rowfac=sample(letters[10 + (1:3)],nobs,replace=TRUE)) %>%
mutate(x=seq(0,1,length.out=nobs) + 0.33 * grp) %>%
mutate(y=0.25*rnorm(nobs) + 2*grp) %>%
mutate(grp=factor(grp)) %>%
mutate(se=sqrt(x)) %>%
mutate(ymin=y-se,ymax=y+se)
offs <- 2
ph <- mydat %>%
mutate(y=y+offs,ymin=ymin+offs,ymax=ymax+offs) %>%
ggplot(aes(x=x,y=y,ymin=ymin,ymax=ymax,color=grp,fill=grp)) +
facet_grid(rowfac ~ colfac) +
scale_y_sqrt() + geom_line() +
geom_cloud(aes(fill=grp),steps=15,max_alpha=0.85,color=NA) +
labs(title='geom cloud')
print(ph)
```
## log-like transforms
The square root transform is a good compromise between raw and logarithmic
scales, showing detail across different scales without over-emphasizing very
small variation. However, it does not work for negative numbers. Thus
a signed square root transform is useful. Along similar lines, the
[pseudo-log transform](http://www.win-vector.com/blog/2012/03/modeling-trick-the-signed-pseudo-logarithm/)
accepts negative numbers while providing a good view across magnitudes.
Some illustrations:
```{r loglike_trans,cache=TRUE,eval=TRUE,echo=TRUE,dpi=200,out.width='600px',out.height='500px'}
library(ggplot2)
library(ggallin)
library(dplyr)
nobs <- 100
# this is a silly example, don't blame me
set.seed(1234)
mydat <- data.frame(x=rnorm(nobs),z=rnorm(nobs)) %>%
mutate(y=sign(z) * exp(x+z-2))
ph <- mydat %>%
ggplot(aes(x=x,y=y)) +
geom_line() +
scale_y_continuous(trans=ssqrt_trans)
print(ph)
ph <- mydat %>%
ggplot(aes(x=x,y=y)) +
geom_line() +
scale_y_continuous(trans=pseudolog10_trans)
print(ph)
```
## interpolated transforms
Scale transforms are useful for 'straightening out' crooked data graphically.
Sometimes these transforms can not be expressed functionally but instead rely
on data. In this case we can imagine that we have some paired data that
provide the transformation _x -> y_. We provide a scale transformation that
supports linear interpolation.
We also provide another scale transformation that accepts _x_ and positive 'weights'
_w_, and computes _y_ by taking the cumulative sum of weights, called a 'warp'
transformation.
Here we illustrate the warp transformation by plotting the cumulative return of
the 'UMD' factor against a time scale that is uniform in cumulative daily VIX
(whatever that means):
```{r interp_trans,cache=TRUE,eval=TRUE,echo=TRUE,dpi=200,out.width='600px',out.height='500px'}
library(ggplot2)
library(ggallin)
library(dplyr)
library(aqfb.data)
library(scales)
data(dvix)
data(dff4)
rr_to_nav <- function(x) {
exp(cumsum(log(1 + x)))
}
rets <- dff4 %>%
as.data.frame() %>%
tibble::rownames_to_column(var='date') %>%
inner_join(dvix %>%
as.data.frame() %>%
setNames(c('VIX')) %>%
tibble::rownames_to_column(var='date'),by='date') %>%
mutate(date=as.Date(date,format='%Y-%m-%d')) %>%
mutate(UMD_nav=rr_to_nav(0.01*UMD),
SMB_nav=rr_to_nav(0.01*SMB),
HML_nav=rr_to_nav(0.01*HML))
ph <- rets %>%
ggplot(aes(x=date,y=UMD_nav)) +
geom_line() +
labs(y='UMD cumulative return') +
labs(x='regular date scale')
print(ph)
# select breaks automagically
ph <- rets %>%
ggplot(aes(x=date,y=UMD_nav)) +
geom_line() +
scale_x_continuous(trans=warp_trans(x=rets$date,w=rets$VIX)) +
labs(y='UMD cumulative return') +
labs(x='warped date scale')
print(ph)
# force decade breaks:
ph <- rets %>%
ggplot(aes(x=date,y=UMD_nav)) +
geom_line() +
scale_x_continuous(trans=warp_trans(x=rets$date,w=rets$VIX,
breaks=scales::date_breaks('10 years'),
format=scales::date_format('%Y'))) +
labs(y='UMD cumulative return') +
labs(x='warped date scale')
print(ph)
# reverse scale as well (see composition of transforms)
ph <- rets %>%
ggplot(aes(x=date,y=UMD_nav)) +
geom_line() +
scale_x_continuous(trans=scales::reverse_trans() %of% warp_trans(x=rets$date,w=rets$VIX)) +
labs(y='UMD cumulative return') +
labs(x='reversed, warped date scale')
print(ph)
```
## composition of transforms
The `%of%` binary operator supports composition of scale transformations. This
is most useful for composing reverse scales with other transforms:
```{r compose_trans,cache=TRUE,eval=TRUE,echo=TRUE,dpi=200,out.width='600px',out.height='500px'}
library(ggplot2)
library(ggallin)
# reverse and log scale
set.seed(1234)
ph <- ggplot(data.frame(x=rnorm(100),y=exp(rnorm(100,mean=-2,sd=4))),aes(x=x,y=y)) +
geom_point() +
scale_y_continuous(trans=scales::reverse_trans() %of% scales::log10_trans()) +
labs(title='reversed and log scaled y')
print(ph)
```