-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme.Rmd
254 lines (174 loc) · 5.13 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE,echo=FALSE}
knitr::opts_chunk$set(
echo = F, message = F, warning = F,
fig.path = "man/figures/",
collapse = T,
comment = "#>"
)
library(jsmp)
```
# jsmp
<br>
This package does variety of things I find useful when working with R Tidyverse:
- [Import commonly used packages](#imported-packages)
- [Changes the default ggplot theme](#custom-ggplot-theme)
- [Overrides dplyr::summarise to remove leftover grouping](#general-functions)
- [Contains various functions for use with ggplot](#ggplot-functions)
- [Contains various functions for use in tidy code](#general-functions)
<br>
## Installation
`devtools::install_github("ymer/jsmp")`
<br>
## Custom ggplot theme
```{r, fig.width = 4, fig.height = 3}
mtcars |> ggplot(aes(x = mpg, y = disp)) +
geom_point()
```
<br>
## ggplot functions
- `gg_hist_percent` : Draws a histogram with y-axis as a percentage
- `gg_legend_remove` : Removes the legend
- `gg_legend_notitle` : Removes the legend title
- `gg_legend_bottom` : Moves the legend to the bottom of the plot
- `gg_loess_line` : Plots a locally weighed regression line
- `gg_regression_line` : Plots a regression line
- `gg_x_remove` : Removes the x-axis
- `gg_x_rotate` : Rotates x-axis labels 45°
- `gg_x_wrap` : Wraps text on x-axis labels
- `gg_y_big` : Changes the y-axis to avoid scientific notation for big numbers
- `gg_y_percent` : Changes the y-axis to percentage
- `gg_y_percent_zero` : Changes the y-axis to percentage, and adjusts the y-axis to start at 0.
- `gg_y_remove` : Removes the y-axis
- `gg_y_zero` : Adjusts the y-axis to start at exactly 0
<br>
## General functions
- `summarise` : Overrides the standard summarise function, so that leftover grouping is dropped after summarising
```{r, echo = T}
mtcars |>
group_by(am, gear) |>
dplyr::summarise(mean_mpg = mean(mpg)) |>
group_vars()
```
```{r, echo = T}
mtcars |>
group_by(am, gear) |>
summarise(mean_mpg = mean(mpg)) |>
group_vars()
```
<br>
- `%notin%` : As `%in%` but exclusionary
<br>
- `d` : Formats table (using `gt` as base)
<br>
- `ci_means` : Finds the means and confidence intervals
```{r, echo = T, fig.width = 4, fig.height = 3}
ToothGrowth |>
group_by(supp) |>
ci_means(len) |>
ggplot(aes(y = supp, x = mean)) +
geom_crossbar(aes(xmin = ci.lower, xmax = ci.upper), width = 0.3, size = 0.7, color = c1)
```
<br>
- `ci_proportions` : Finds the proportions and confidence intervals
```{r, echo = T, fig.width = 4, fig.height = 3}
mtcars |>
mutate(cyl = as.character(cyl)) |>
count(cyl) |>
mutate(total = sum(n)) |>
ci_proportions() |>
ggplot(aes(x = cyl, y = proportion)) +
geom_col(position = position_dodge()) +
gg_y_percent_zero() +
gg_legend_notitle() +
labs(y = "", x = "") +
geom_errorbar(aes(ymin = ci.lower, ymax = ci.upper),
width=.2, position=position_dodge(.9))
```
<br>
- `do_if` : Use a condition in a pipe flow
```{r, echo = T}
only_high_values <- T
mtcars |> do_if(only_high_values, ~ . |> filter(disp > 180)) |>
summarise(mean(disp))
```
```{r, echo = T}
only_high_values <- F
mtcars |> do_if(only_high_values, ~ . |> filter(disp > 180)) |>
summarise(mean(disp))
```
```{r, echo = T}
high_or_low <- "low"
mtcars |> do_if(high_or_low == "high",
~ .x |> filter(disp >= 180),
~ .x |> filter(disp < 180)) |>
summarise(mean(disp))
```
<br>
- `filter_duplicates` : selects duplicated rows
```{r, echo = T}
mtcars |> filter_duplicates(wt)
```
<br>
- `fix_names` : Changes the column names to tidy style
```{r, echo = T}
iris |> fix_names() |>
head()
```
<br>
- `left_join0` : Performs a left_join, while setting values in missing rows to 0 instead of NA.
```{r, echo = T}
df1 <- tribble(
~id, ~v1,
1, 2,
2, 2,
3, 10)
df2 <- tribble(
~id, ~v2,
1, 2,
3, 4)
left_join(df1, df2)
```
```{r, echo = T}
left_join0(df1, df2)
```
<br>
- `percent` : Returns proportion formatted as percentage
```{r, echo = T}
percent(0.173234235)
```
<br>
- `rows` : Facilitates looping
```{r, echo = T}
df <- mtcars |> head()
for (row in rows(df)){
print(row$mpg)
}
```
<br>
- `tab` : Ordered `count` including percentage
```{r, echo = T}
ToothGrowth |>
filter(len > 20) |>
tab(supp)
```
<br>
- `transpose` : Flips rows and columns
```{r, echo = T}
mtcars |>
rownames_to_column() |>
transpose()
```
<br>
## Imported packages
- [broom](https://github.com/tidymodels/broom)
- [glue](https://github.com/tidyverse/glue)
- [gt](https://github.com/rstudio/gt)
- [lubridate](https://github.com/tidyverse/lubridate)
- [magrittr](https://github.com/tidyverse/magrittr)
- [patchwork](https://github.com/thomasp85/patchwork)
- [stringr](https://stringr.tidyverse.org/)
- [tidyverse](https://github.com/tidyverse/tidyverse)