rm(list=ls())
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 503798 27.0    1117301 59.7   644242 34.5
## Vcells 887973  6.8    8388608 64.0  1635093 12.5
ls()
## character(0)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.2.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
bd <- read_excel("C:/Users/Julietha Zorro M/Downloads/FE.xlsx")
View(bd)

El gráfico que ustedes presentan es el siguiente

bd[, -1] <- sapply(bd[, -1], as.numeric)

suma_por_año <- apply(bd[, -1], 1, sum)

label <- paste(bd$AÑO, "\n", "Total:", suma_por_año)

a <- pie(suma_por_año, labels = label, main = "Distribución del total por año")

Sin embargo, esta no es una buena forma de presentar los datos para totales anuales.

Se recomienda una gráfica de barras. Tener en cuenta que, cuando una variable tiene espacios en su nombre para reconocerla en RStudio es necesario que este entre estas comillas `. Ejemplo:TOTAL GENERAL`

Ahora, ¿cómo quitarian la barra del TOTAL?. Es necesario dejarla?

names(bd)
##  [1] "AÑO"                         "ESCLAVITUD"                 
##  [3] "EXPLOTACIÓN SEXUAL"          "MATRIMONIO SERVIL"          
##  [5] "MENDICIDAD AJENA"            "NO REGISTRA"                
##  [7] "OTRAS FORMAS DE EXPLOTACIÓN" "SERVIDUMBRE"                
##  [9] "TRABAJOS FORZADOS"           "TOTAL GENERAL"
bd$AÑO <- as.factor(bd$AÑO)


plot1 <- ggplot(bd, aes(x = AÑO, y = `TOTAL GENERAL`)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Total xxx",
       subtitle = "Años xxx a xxx",
       caption = "Fuente: ?¿",
       x = "Año",
       y = "Número de personas ?¿")+
  geom_text(aes(label = round(`TOTAL GENERAL`,digits = 1)), vjust = -0.5, color = "black", size = 2) +  # Agrega etiquetas de texto
  theme_light()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1,size=7.5))
  

plot1

Ejemplo, de un gráfico circular para el año 2022.

  1. Filtra la base de datos solo para el año 2022
  2. Transpone la base de datos para poder crear el gráfico 3 Crea el gráfico
#install.packages(ggrepel)
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.2.3
#install.packages("forcats")
library(forcats)
## Warning: package 'forcats' was built under R version 4.2.3
library(tidyr)

table(bd$AÑO)
## 
##   2013   2014   2015   2016   2017   2018   2019   2020 2021.0 2022.0  TOTAL 
##      1      1      1      1      1      1      1      1      1      1      1
FE_2022 <- bd %>% filter(AÑO=="2022.0")
View(FE_2022)

FE_año1<- FE_2022 %>%
  select(AÑO,ESCLAVITUD,`EXPLOTACIÓN SEXUAL`,`MATRIMONIO SERVIL`,`MENDICIDAD AJENA`,`NO REGISTRA`,`OTRAS FORMAS DE EXPLOTACIÓN`,SERVIDUMBRE,`TRABAJOS FORZADOS`)%>%
  pivot_longer(cols=c(ESCLAVITUD,`EXPLOTACIÓN SEXUAL`,`MATRIMONIO SERVIL`,`MENDICIDAD AJENA`,`NO REGISTRA`,`OTRAS FORMAS DE EXPLOTACIÓN`,SERVIDUMBRE,`TRABAJOS FORZADOS`),
               names_to='Tipo de explotación',
               values_to='Total')%>%
  arrange(desc(Total))%>%
  mutate(Porcentaje = round((Total/sum(Total)*100),digits = 1))%>%
  # Obtener las posiciones de las etiquetas
  mutate(csum = rev(cumsum(rev(Total))), 
         pos = Total/2 + lead(csum, 1),
         pos = if_else(is.na(pos), Total/2, pos))


ggplot(filter(FE_año1,Total>0), aes(x="", y=Porcentaje,fill=fct_inorder(`Tipo de explotación`))) +
  geom_col(width=1,color=1) +
  coord_polar(theta = "y") +
  geom_label_repel(aes(y = pos, label = paste0(`Tipo de explotación`,"\n",Porcentaje, "%")),
                   size = 2.5, nudge_x = 1, show.legend = T)+
  theme_void()+
  theme(legend.position = "none")