# WARNING - Generated by {fusen} from /dev/flat_minimal.Rmd: do not edit by hand
#' Funcion para realizar un grafico donut.
#'
#' @param data Base de datos para la funcion
#' @param var Variable para gráfico donut
#' @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 donut
#' @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_donut<- function(data, var, filtrar=TRUE, paleta=1, direction=1) {
total<-
data %>%
nrow()
tag<-
data %>%
filter({{filtrar}}) %>%
sjlabelled::as_label() %>%
select({{var}}) %>%
nrow()
data %>%
filter({{filtrar}}) %>%
sjlabelled::as_label() %>%
group_by({{var}}) %>%
count() %>%
rename(count=n) %>%
ungroup() %>%
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=1, fill={{var}})) +
geom_rect() +
geom_text( x=2.5, aes(y=labelPosition, label=label),
hjust="middle",
size = 3.5,
fontface = "bold",
color = "#002060",
family="sans") +
scale_fill_brewer(type="seq",palette=paleta, direction=direction) +
coord_polar(theta="y") +
xlim(c(-1, 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)} )
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.