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_Educacion_ <- read_excel("C:/Users/Julietha Zorro M/Downloads/BD (Educacion).xlsx")
View(BD_Educacion_)
Se carga la información de Terridata y se filtra para unos municipios en especifico.
En la base filas_separadas2 se presenta optimizado el código, los dos dan el mismo resultado:
filas_separadas <- BD_Educacion_ %>%
filter(Entidad == "Bogotá" | Entidad == "Medellín" | Entidad == "Cali" | Entidad == "Barranquilla" | Entidad == "Cartagena"
| Entidad == "Bucaramanga" | Entidad == "Pasto" | Entidad == "Soacha" | Entidad == "Cúcuta" | Entidad == "Soledad")
dim(filas_separadas)
## [1] 10 15
filas_separadas2 <- BD_Educacion_ %>%
filter(Entidad %in% c("Bogotá","Medellín","Cali","Barranquilla","Cartagena", "Bucaramanga","Pasto","Soacha","Cúcuta","Soledad"))
dim(filas_separadas2)
## [1] 10 15
El código usado por los estudiantes es:
plot1 <- ggplot(filas_separadas, aes(x = Entidad, y = `Tasa de Analfabetismo`)) +
geom_bar(stat = "identity", fill = "yellow") +
labs(title = "Tasa de Analfabetismo",
x = "Entidad",
y = "Tasa de Analfabetismo") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot1
Se ajusta el codigo para ordenar de mayor a menor por Tasa de Analfabetismo y para marcar de un color diferente a Bogotá
# Ordenar el dataframe por la columna Tasa de Analfabetismo de manera descendente
filas_separadas <- filas_separadas[order(filas_separadas$`Tasa de Analfabetismo`, decreasing = TRUE), ]
# Convertir la variable Entidad en un factor con el orden deseado
filas_separadas$Entidad <- factor(filas_separadas$Entidad, levels = filas_separadas$Entidad)
# Marcar a Bogotá con un color diferente
filas_separadas$Color <- ifelse(filas_separadas$Entidad == "Bogotá", "red", "yellow")
# Crear el gráfico de barras
plot1 <- ggplot(filas_separadas, aes(x = Entidad, y = `Tasa de Analfabetismo`, fill = Color)) +
geom_bar(stat = "identity") +
labs(title = "Tasa de Analfabetismo por Entidad ",
subtitle = "Año xxx",
caption = "Fuente: Terridata?¿",
x = "Municipio",
y = "Tasa de Analfabetismo (%)") +
geom_text(aes(label = paste(round(`Tasa de Analfabetismo`,digits = 2),"%")), vjust = -0.5, color = "black", size = 2.5) + # Agrega etiquetas de texto
theme(axis.text.x = element_text(angle = 45, hjust = 1,size=8.5)) +
guides(fill = FALSE) # Oculta la leyenda de color
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Mostrar el gráfico
print(plot1)