R/grafico_pie.R

Defines functions grafico_pie

Documented in grafico_pie

# 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)} )
  
  
}
aito123/pulso documentation built on June 21, 2022, 4:32 p.m.