VADeaths
# Visualizando o dataset
print(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
# Definindo 5 cores (uma para cada uma das 5 faixas etárias/linhas do dataset)
cores <- c("#E74C3C", "#3498DB", "#2ECC71", "#F39C12", "#9B59B6")
# Gráfico de barras agrupadas (beside = TRUE)
barplot(
VADeaths,
beside = TRUE,
col = cores,
main = "Taxa de Mortalidade na Virgínia (1940)",
xlab = "Grupo Populacional",
ylab = "Taxa de Mortalidade (por 1000 habitantes)",
ylim = c(0, 85),
legend.text = rownames(VADeaths),
args.legend = list(
x = "topright",
title = "Faixa Etária",
bty = "n",
cex = 0.85
)
)

ClassificaçãoDoença
# Dados dos 20 pacientes
dados <- c("moderado","leve","leve","severo","leve",
"moderado","moderado","moderado","leve","leve",
"severo","leve","moderado","moderado","leve",
"severo","moderado","moderado","moderado","leve")
# Contagem por categoria
contagem <- table(dados)
contagem <- contagem[c("leve","moderado","severo")] # ordenar do leve ao severo
# Porcentagens
pct <- round(100 * contagem / sum(contagem), 1)
rotulos <- paste0(pct, "%")
# Cores
cores_pizza <- c("#2ECC71", "#F39C12", "#E74C3C")
# Gráfico de pizza
pie(
contagem,
labels = rotulos,
col = cores_pizza,
main = "Classificação da Doença em 20 Pacientes",
radius = 0.9
)
# Legenda
legend(
"bottomright",
legend = names(contagem),
fill = cores_pizza,
title = "Estágio",
bty = "n",
cex = 0.9
)

USArrests
library(plotly)
library(dplyr)
df <- USArrests %>%
mutate(City = rownames(USArrests)) %>%
arrange(desc(Murder)) %>%
slice(1:5)
df$City <- factor(df$City, levels = df$City)
fig <- plot_ly(df, x = ~City, y = ~Murder, type = "bar",
name = "Murder", marker = list(color = "#E74C3C")) %>%
add_trace(y = ~Rape, name = "Rape", marker = list(color = "#3498DB")) %>%
add_trace(y = ~Assault, name = "Assault", marker = list(color = "#2ECC71")) %>%
layout(
title = "Top 5 Estados Mais Violentos dos EUA",
barmode = "group",
xaxis = list(title = "Cidade (Estado)"),
yaxis = list(title = "Ocorrências por 100.000 habitantes"),
legend = list(title = list(text = "Tipo de Crime"))
)
fig
Orange
library(plotly)
Orange$Tree <- factor(Orange$Tree, levels = 1:5)
fig2 <- plot_ly(
data = Orange,
x = ~age,
y = ~circumference,
color = ~Tree,
colors = c("#E74C3C", "#3498DB", "#2ECC71", "#F39C12", "#9B59B6"),
type = "scatter",
mode = "lines+markers",
text = ~paste0("Árvore ", Tree, " — idade: ", age, " dias — circ.: ", circumference, " mm"),
hoverinfo = "text"
) %>%
layout(
title = "Crescimento das Laranjeiras ao Longo do Tempo",
xaxis = list(title = "Idade (dias)"),
yaxis = list(title = "Circunferência do Tronco (mm)"),
legend = list(title = list(text = "Árvore"))
)
fig2