-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
281 lines (209 loc) · 8.56 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit the .Rmd file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse=TRUE,
comment="#>",
strip.white=FALSE,
tidy=FALSE,
dpi=300,
fig.align="center",
fig.path="man/figures/README-",
fig.width=5.25,
fig.height=3.8,
out.width="700px"
)
```
[](https://cran.r-project.org/package=ggdistribute)
# ggdistribute
A `ggplot2` Extension for Plotting Unimodal Distributions
The `ggdistribute` package is an extension for plotting posterior or other types of unimodal distributions that require overlaying information about a distribution's intervals. It makes use of the `ggproto` system to extend `ggplot2`, providing additional "geoms", "stats", and "positions." The extensions integrate with existing `ggplot2` layer elements.
## Example
```{r, echo=FALSE, message=FALSE, results='hide', warning=FALSE}
library(ggplot2)
devtools::load_all(".", export_all = FALSE)
theme_set(theme_mejr())
```
The package function `example_plot()` is an overview of combining `ggdistribute` with other `ggplot2` elements. The contents of this function are printed below and gives details about the extended parts to `ggplot2`.
```{r, eval=FALSE}
library(ggplot2)
library(ggdistribute)
```
```{r turtle_snails, echo = FALSE, fig.width=5, fig.height=3, out.width="100%"}
plot(example_plot())
```
```{r prin_ex_plot, echo=FALSE, results='asis'}
ggdistribute:::function2chunk("example_plot")
```
## Additional examples from an arbritrary dataset
```{r}
# total number of samples in the dataset
N <- 2500
means <- c(-1, 2, 3, 5)
```
The `data` object below is a randomly generated dataset of `r length(means)` different normal distributions. Two factors, `Condition` and `Group`, are assigned to subsets of the generated values. `r N` samples are generated for each value of `mu` for a total of $ `r N*length(means)` $ rows.
```{r norm_data}
data <- data_normal_sample(mu = means, n = N)
```
Create a new grouping variable called `Level` based on the column `Group`.
```{r norm_data_grps}
# number of levels to make
num_levels <- 8L
# R version >= 3.5 now let's you assign factors this way.
data$Level <- with(data, factor(
Group,
levels = letters[seq_len(num_levels)],
labels = c(rep("Low", 3), rep("Mid", 2), rep("High", 3)),
ordered = TRUE
))
```
Show unique groups per `Group`, `Condition`, and `Level` to help understand the data factors.
```{r}
unique(data[, c("Group", "Condition", "Level")])
```
### Facetting and spreading groups
```{r space_ships, fig.width=5, fig.height=4}
ggplot(data) +
aes(x=value, y=Condition, group=Group) +
geom_posterior(
aes(fill=Level),
mirror=TRUE,
show.legend=FALSE,
adjust=1.5,
brighten=c(6, 0, 2.5),
position=position_spread(reverse=TRUE)
) +
geom_point(
aes(color=Level, shape=Condition),
alpha=.08,
fill=NA,
show.legend=FALSE,
position=position_jitter(0, .45)
) +
coord_cartesian(ylim=c(0.5, 2.5), expand=FALSE) +
facet_wrap(~ Level, scales="free") +
labs(title="Space Invaders", y="Condition", x="Parameter estimate")
```
### Changing the appearance of `geom_posterior`
```{r candy_wrappers, fig.width=5, fig.height=4}
ggplot(data) +
aes(x=value, y=Group) +
geom_vline(
xintercept=0, size=.6
) +
geom_posterior(
aes(color=Condition),
midline=NULL,
mirror=TRUE,
fill="#FFFFFF",
draw_sd=FALSE,
interval_type="hdi",
vjust=0,
position=position_spread(height=2)
) +
labs(
title="Candy Wrappers",
x="Parameter estimate",
y="Sample location"
) +
scale_x_continuous(breaks=seq(-10, 10, 1)) +
theme(
legend.position=c(.025, .9),
legend.justification=c(0, 0),
panel.grid.major.y=element_line(color=gray(.92))
)
```
### The *y* axis is a repeated, continuous grouping variable
The variable `GroupScore` is a continuous variable assigned to each `Group`. The distributions will be positioned at the start of the y value for each group, and resized to not overlap with the next group. Resizing can be overriden by specifying `height` in `position_spread`.
```{r y_cont}
unique(data[, c("Group", "GroupScore")])
```
```{r rainbow_hills, fig.width=5, fig.height=6}
ggplot(data) +
aes(x=value, y=GroupScore) +
geom_vline(
xintercept=0, size=.6
) +
geom_posterior(
aes(fill=Group),
midline="#FFFFFF",
colour="#FFFFFF",
alpha=0.7,
brighten=c(1.3, 0, -1.3),
interval_type="hdi",
position=position_spread(height=0.5, padding=0)
) +
labs(
title="Rainbow Hills",
x="Parameter estimate",
y="Group's score"
) +
scale_x_continuous(breaks=seq(-10, 10, 1)) +
scale_y_continuous(breaks=seq(-10, 10, .5))
```
## How to install
### Dependencies
- R: <https://www.r-project.org/>
A current R installation.
#### Dependencies for installing the development version of this package
- `devtools` package: <https://www.rstudio.com/products/rpackages/devtools/>
The `devtools` package is an R package that makes it easier to install local or remote content as an R package that can be used like any other standard R package. You can install `devtools` by opening up RStudio or an R terminal and running
```{r devtools-inst, eval=FALSE}
install.packages("devtools")
```
For Windows users, you *may* be required to install Rtools first before you can use the `devtools` package, if there is any code that needs to be compiled. These are a set of build tools customized for building R packages (see the `devtools` link above for more details).
- Build tools: <http://cran.r-project.org/bin/windows/Rtools/>
### Installing from CRAN
If you want to use the last version that was uploaded to the CRAN repository, do the following:
```{r cran-inst, eval=FALSE}
install.packages("ggdistribute")
```
### Installing from the downloaded package content folder
If you have all of the `ggdistribute` package contents (e.g., an unzipped folder containing `DESCRIPTION`, `NAMESPACE`, `R/`, etc...), you can open up the `ggdistribute.Rproj` file in RStudio and use both `devtools` and RStudio to load or install package.
The first step is to make sure you have all the package dependencies (other packages that this pacakge relies on) to be able to load or install the `ggdistribute` package materials. You can run the line below to install dependencies first.
```{r local-inst-dep, eval=FALSE}
devtools::install_dev_deps()
```
After the dependencies are installed, you can now build and install `ggdistribute` from the current working directory.
Assuming the `ggdistribute` project is loaded in RStudio, you can leave out the first argument.
```{r local-inst, eval=FALSE}
devtools::install()
```
If installing from a different working directory, enter the path of the package contents to manually specify what to install.
```{r local-inst-alt, eval=FALSE}
devtools::install_dev_deps("/Path/to/the/folder/ggdistribute")
devtools::install("/Path/to/the/folder/ggdistribute")
```
### Installing from GitHub
If `devtools` are installed, you may use the `install_github()` function to download and install the development version of the package from this GitHub repository instead of the one hosted on CRAN. Run the code below to download and install the development version:
```{r gh-installation, eval = FALSE}
devtools::install_github("iamamutt/ggdistribute")
```
or to install all suggested packages as well...
```{r gh-installation-dep, eval = FALSE}
devtools::install_github("iamamutt/ggdistribute", dependencies=TRUE)
```
### Loading the package
If successful, the package should now be installed and can be loaded as any other package. Repeat the last intall step if there are updates to the package, or complete all steps to install on another machine. You should now be able to use the package materials and should see it in your packages tab if using RStudio. It should be loaded like any other package.
```{r load-pkg, eval=FALSE}
library(ggdistribute)
```
## Getting help
### Browsing the vignettes
Vignettes can be viewed in several different ways.
- pre-built and saved in the `inst\doc` folder on GitHub.
- calling `vignette("geom_posterior", "ggdistribute")` from within R after the package is installed.
- navigating to packages tab > ggdistribute > User guides, package vignettes... in RStudio.
### Viewing the help documentation
View the package welcome page to navigate to different types of help documents
```{r show-welcome, eval=FALSE}
package?ggdistribute
```
Viewing package information and a list of exported objects:
```{r show-help, eval=FALSE}
help(package = "ggdistribute")
# or
library(help="ggdistribute")
```