-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrafico_pie.R
117 lines (103 loc) · 2.93 KB
/
grafico_pie.R
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
# WARNING - Generated by {fusen} from /dev/flat_minimal.Rmd: do not edit by hand
#' Funcion para realizar un grafico pie
#'
#' @param data Base de datos para la funcion
#' @param var Variable para gráfico pie
#' @import tidyverse
#' @import glue
#' @import sjlabelled
#' @import testthat
#' @import janitor
#' @import glue
#' @import lubridate
#' @import scales
#' @import Hmisc
#' @import lazyeval
#' @import plotly
#' @import ggrepel
#' @import cowplot
#' @import grid
#' @import fmsb
#' @import haven
#' @import rio
#' @import officer
#' @import officedown
#' @import sjlabelled
#' @import flextable
#' @import knitr
#' @import kableExtra
#' @import DT
#' @import gtsummary
#' @import ggpubr
#' @import paletteer
#' @import RColorBrewer
#' @import grDevices
#' @import graphics
#' @import utils
#' @return Un grafico pie
#' @examples
#'
#' # data_prueba_ruta <- system.file("data_prueba.sav", package = "pulso")
#' # data_prueba <- haven::read_sav(data_prueba_ruta)
#' #
#' # data_prueba %>%
#' # grafico_donut(sexo)
#'
#' @export
grafico_pie<- function(data, var, filtrar=TRUE, color1="#A9D18E",color2="#FF9393") {
total<-
data %>%
nrow()
tag<-
data %>%
filter({{filtrar}}) %>%
sjlabelled::as_label() %>%
select({{var}}) %>%
nrow()
data<-
data %>%
filter({{filtrar}}) %>%
sjlabelled::as_label() %>%
group_by({{var}}) %>%
count() %>%
rename(count=n) %>%
ungroup()
colores<-colorRampPalette(c(color1,color2))
num_colores<-
data %>%
nrow()
color=colores(num_colores)
data %>%
mutate(prop=paste0(round_half_up(count/sum(count), digits = 2)*100, "%"),
# Compute percentages
fraction=count/sum(count),
# Compute the cumulative percentages (top of each rectangle)
ymax=cumsum(fraction),
# Compute the bottom of each rectangle
ymin=c(0, head(ymax, n=-1)),
# Compute label position
labelPosition=(ymax + ymin) / 2,
# Compute a good label
label=paste0({{var}}, "\n ", prop),
) %>%
# Make the plot
ggplot(aes(ymax=ymax, ymin=ymin, xmax=4, xmin=0, fill={{var}})) +
geom_rect(colour="white", size=0.7) +
geom_text( x=2.5, aes(y=labelPosition, label=label),
hjust="middle",
size = 3.5,
fontface = "bold",
color = "#002060",
family="sans") +
scale_fill_manual(values=c(color)) +
coord_polar(theta="y", start=0) +
xlim(c(0, 4)) +
theme_void() +
theme(legend.position = "none",
text = element_text(size = 9, color="#002060",family="sans"),
plot.caption = element_text(face = "italic",family="sans"),
plot.tag = element_text(size = 8, color="grey40"),
plot.tag.position = "topright") +
labs(caption = "Elaborado por Pulso PUCP",
tag = if(tag == total) {glue("N=",tag)} else {glue("N=",tag,"/",total)} )
}