VADeaths
# Visualizar o dataset
VADeaths
## Rural Male Rural Female Urban Male Urban Female
## 50-54 11.7 8.7 15.4 8.4
## 55-59 18.1 11.7 24.3 13.6
## 60-64 26.9 20.3 37.0 19.3
## 65-69 41.0 30.9 54.6 35.1
## 70-74 66.0 54.3 71.1 50.0
# Carregar pacotes
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
# Converter VADeaths para data frame longo (tidy)
df <- as.data.frame(VADeaths)
df$Grupo <- rownames(df)
df_long <- df %>%
pivot_longer(
cols = c("Rural Male", "Rural Female", "Urban Male", "Urban Female"),
names_to = "Categoria",
values_to = "Taxa"
)
# Criar o grƔfico de barras empilhadas com barras agrupadas
ggplot(df_long, aes(x = Grupo, y = Taxa, fill = Categoria)) +
geom_bar(stat = "identity", position = "stack") +
facet_grid(. ~ Categoria, scales = "free_x", space = "free") +
labs(
title = "Taxas de Mortalidade - VADeaths",
x = "Grupo",
y = "Taxa por 100.000 habitantes",
fill = "Categoria"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1)
)
