-
Notifications
You must be signed in to change notification settings - Fork 3
/
ggpie.Rmd
375 lines (296 loc) · 12.9 KB
/
ggpie.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
---
title: "ggpie"
author: "Yabing Song"
date: "`r Sys.Date()`"
output: prettydoc::html_pretty
vignette: >
%\VignetteIndexEntry{Introduction to ggpie}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dpi=60
)
```
# ggpie - A ggplot2 extension to create pie, donut and rose pie plot
<img src = "../man/figures/ggpie_hex.png" align = "right" width = "200"/>
## Introduction
`ggpie` aims to create **pie (2D and 3D)**, **donut** and **rose pie** plot with the `ggplot2` plotting system which implemented the grammar of graphics. It contains five main functions:
* `ggpie`: Create 2D pie plot with single group variable.
* `ggdonut`: Create 2D donut plot with single group variable.
* `ggnestedpie`: Create 2D nested pie plot with two group variables.
* `ggpie3D`: Create 3D pie plot with single group variable.
* `ggrosepie`: Create rose pie plot with single or two group variables.
<hr />
## Installation
You can install the released version of `ggpie` from [CRAN](https://cran.r-project.org/package=ggpie) with:
```{r cran_install, eval=FALSE, message=FALSE, warning=FALSE}
install.packages("ggpie")
```
Or install the package via the [Github repository](https://github.com/showteeth/ggpie):
```{r github_install, eval=FALSE, message=FALSE, warning=FALSE}
# install.package("remotes") #In case you have not installed it.
remotes::install_github("showteeth/ggpie")
```
<hr />
## Citation
```{r citation}
citation("ggpie")
```
<hr />
## Usage
### Preapre data
```{r prepare, message=FALSE, warning=FALSE}
library(ggpie)
library(ggplot2)
data(diamonds)
# check data used
str(diamonds)
```
<hr />
### Pie plot
#### no label
Pie plot with **no label**:
```{r pie_basic_no_label}
# with no label
ggpie(data = diamonds, group_key = "cut", count_type = "full",label_type = "none")
```
#### circle label
**circle label** and **out** of pie:
```{r pie_circle_out}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "circle",
label_size = 4, label_pos = "out")
```
**circle label** and **in** pie plot, with **no split**:
```{r pie_circle_in_no_split}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "circle", label_split = NULL,
label_size = 4, label_pos = "in")
```
#### horizon label
**horizon label** and **in** pie plot, with **no split**:
```{r pie_horizon_in_no_split}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon", label_split = NULL,
label_size = 4, label_pos = "in")
```
**horizon label** and **in** pie plot, **split with space**:
```{r pie_horizon_in_split}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon",
label_size = 4, label_pos = "in")
```
**horizon label** and **out** pie plot, with **no split**:
```{r pie_horizon_out_no_split}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon",
label_size = 4, label_pos = "out" )
```
#### adjust threashold
With `label_threshold`, `ggpie` will move label below `label_threshold` to out of pie. In this example, all labels below 10% are moved to out of pie:
```{r pie_threashold_horizon_in}
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon", label_split = NULL,
label_size = 4, label_pos = "in", label_threshold = 10)
```
<hr />
### Donut plot
#### no label
```{r donut_basic_no_label}
# with no label
ggdonut(data = diamonds, group_key = "cut", count_type = "full",label_type = "none")
```
#### circle label
**circle label** and **out** of pie:
```{r donut_circle_out}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "circle",
label_size = 4, label_pos = "out")
```
**circle label** and **in** pie plot, with **no split**:
```{r donut_circle_in_no_split}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "circle", label_split = NULL,
label_size = 4, label_pos = "in")
```
#### horizon label
**horizon label** and **in** pie plot, with **no split**:
```{r donut_horizon_in_no_split}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon", label_split = NULL,
label_size = 4, label_pos = "in")
```
**horizon label** and **in** pie plot:
```{r donut_horizon_in}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon",
label_size = 4, label_pos = "in")
```
**horizon label** and **out** of pie plot, with **no split**:
```{r donut_horizon_out_no_split}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon", label_split = NULL,
label_size = 4, label_pos = "out")
```
**horizon label** and **out** of pie plot:
```{r donut_horizon_out}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon",
label_size = 4, label_pos = "out")
```
#### adjust threashold
With `label_threshold`, `ggpie` will move label below `label_threshold` to out of donut. In this example, all labels below 10% are moved to out of donut:
```{r donut_threashold_horizon_in}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon", label_split = NULL,
label_size = 4, label_pos = "in", label_threshold = 10)
```
```{r donut_threashold_horizon_in_no_split}
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = c("count", "ratio"), label_type = "horizon",
label_size = 4, label_pos = "in", label_threshold = 10)
```
<hr />
### 3D pie plot
```{r pie3d}
p1= ggpie3D(data = diamonds, group_key = "cut", count_type = "full", tilt_degrees = -10, label_size=2) +
ggtitle("tilt_degrees = -10") +
theme(plot.title = element_text(hjust = 0.5))
p2= ggpie3D(data = diamonds, group_key = "cut", count_type = "full", tilt_degrees = -40, label_size=2) +
ggtitle("tilt_degrees = -40") +
theme(plot.title = element_text(hjust = 0.5))
p3= ggpie3D(data = diamonds, group_key = "cut", count_type = "full", tilt_degrees = -10,
start_degrees = 60, label_size=2) +
ggtitle("start_degrees = 60") +
theme(plot.title = element_text(hjust = 0.5))
p4= ggpie3D(data = diamonds, group_key = "cut", count_type = "full", tilt_degrees = -10,
start_degrees = 180, label_size=2) +
ggtitle("start_degrees = 180") +
theme(plot.title = element_text(hjust = 0.5))
cowplot::plot_grid(p1,p2,p3,p4,ncol = 2)
```
<hr />
### Nested pie/donut plot
It is often the case that we have **two categorical variables** and then need to plot for both of them, `ggnestedpie` is designed to deal with this.
#### inner circle and outer circle
**inner circle label** and **no split**, **outer circle label** and **in** pie plot
```{r nested_pie_inner_outer_circle_in}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all", inner_label_split = NULL,inner_label_size = 2,
outer_label_type = "circle", outer_label_pos = "in", outer_label_info = "all")
```
**inner circle label** and **no split**, **outer circle label** and **in** pie plot, **remove fraction below 1 of inner pie**:
```{r nested_pie_inner_outer_circle_in_remove}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all", inner_label_split = NULL,
inner_label_threshold = 5, inner_label_size = 2,
outer_label_type = "circle", outer_label_pos = "in", outer_label_info = "all")
```
**inner circle label** and **no split**, **outer circle label** and **out** of pie plot:
```{r nested_pie_inner_outer_circle_out}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all", inner_label_split = NULL, inner_label_size = 2,
outer_label_type = "circle", outer_label_pos = "out", outer_label_info = "all")
```
#### inner circle and outer horizon
**inner circle label** and **no split**, **outer horizon label** and **out** of pie plot, **remove fraction below 1 of inner pie**:
```{r nested_pie_inner_outer_horizon_out_remove}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all", inner_label_split = NULL,
inner_label_threshold = 1, inner_label_size = 2,
outer_label_type = "horizon", outer_label_pos = "out", outer_label_info = "all")
```
**inner circle label** and **no split**, **outer horizon label** and **in** pie plot, **remove fraction below 1 of inner pie**, **adjust fraction below 10 to out of pie of outer pie plot**:
```{r nested_pie_inner_outer_remove_horizon_in_remove}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all", inner_label_split = NULL,
inner_label_threshold = 1, inner_label_size = 2,
outer_label_type = "horizon", outer_label_pos = "in",
outer_label_info = "all", outer_label_threshold = 10)
```
#### blank between plot
```{r nested_pie_blank_inner_outer}
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
r0 = 0.5, r1 = 1.5, r2 = 2.6,inner_label_info = "all", inner_label_split = NULL,
inner_label_threshold = 1, inner_label_size = 2,
outer_label_type = "horizon", outer_label_pos = "in",
outer_label_info = "all", outer_label_threshold = 10)
```
<hr />
### Rose pie/donut plot
#### single variable
**do not show tick**:
```{r rose_pie_no_tick, fig.height=10, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
show_tick=F,donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
show_tick=F,donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
**show tick** and **select breaks automaticly**:
```{r rose_pie_with_tick, fig.height=8, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
**show tick** and **with specific breaks**:
```{r rose_pie_with_tick_specific_break, fig.height=8, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
tick_break = c(3000,5000,7000,11000), donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = "color", count_type = "full", label_info = "all",
tick_break = c(3000,5000,7000,11000), donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
#### two variables
**do not show tick**:
```{r rose_pie_two_no_tick, fig.height=8, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
show_tick=F,donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
show_tick=F,donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
**show tick** and **select breaks automaticly**:
```{r rose_pie_two_with_tick, fig.height=8, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
**show tick** and **with specific breaks**:
```{r rose_pie_two_with_tick_specific_break, fig.height=8, fig.width=18}
# pie plot
p1=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
tick_break = c(3000,5000,7000,11000), donut_frac=NULL)
# donut plot
p2=ggrosepie(diamonds, group_key = c("color","clarity"),
count_type = "full", label_info = "all",
tick_break = c(3000,5000,7000,11000), donut_frac=0.3,donut_label_size=3)
cowplot::plot_grid(p1,p2)
```
<hr />
## Contributing
Please note that the `ggpie` project is released with a [Contributor Code of Conduct](https://www.contributor-covenant.org/version/1/0/0/code-of-conduct/). By contributing to this project, you agree to abide by its terms.
<br />