VADeaths

data(VADeaths)

# Uma cor distinta para cada faixa etária (linhas da matriz)
cores <- c("#1b7837", "#762a83", "#e6550d", "#3182bd", "#d6604d")

barplot(
  VADeaths,
  beside = TRUE,
  col    = cores,
  main   = "Taxa de Mortalidade na Virgínia (1940)",
  xlab   = "Grupo Demográfico",
  ylab   = "Taxa de Mortalidade (por 1000 hab.)",
  ylim   = c(0, max(VADeaths) * 1.40),
  las    = 1,
  cex.names = 0.9
)

legend(
  "topright",
  legend = rownames(VADeaths),
  fill   = cores,
  title  = "Faixa Etária",
  bty    = "n"
)

ClassificaçãoDoença

dados <- c("moderado", "leve",    "leve",    "severo",  "leve",
           "moderado", "moderado","moderado","leve",    "leve",
           "severo",   "leve",    "moderado","moderado","leve",
           "severo",   "moderado","moderado","moderado","leve")

tabela <- table(dados)
pct    <- round(100 * tabela / sum(tabela), 1)

cores_pizza <- c("#AED6F1", "#A9DFBF", "#F1948A")   # leve / moderado / severo

pie(
  tabela,
  labels = paste0(pct, "%"),
  col    = cores_pizza,
  main   = "Classificação da Doença em 20 Pacientes",
  cex    = 1.1
)

legend(
  "bottomright",
  legend = paste0(names(tabela), " (", pct, "%)"),
  fill   = cores_pizza,
  title  = "Estágio",
  bty    = "n"
)

USArrests

library(dplyr)
library(plotly)

df        <- USArrests
df$City   <- rownames(USArrests)

top5 <- df |>
  arrange(desc(Murder)) |>
  head(5)

plot_ly(top5, x = ~City) |>
  add_bars(
    y      = ~Murder,
    name   = "Murder",
    marker = list(color = "#4472C4")
  ) |>
  add_bars(
    y      = ~Rape,
    name   = "Rape",
    marker = list(color = "#ED7D31")
  ) |>
  add_bars(
    y      = ~Assault,
    name   = "Assault",
    marker = list(color = "#70AD47")
  ) |>
  layout(
    title   = "5 estados mais violentes (EUA, 1973)",
    xaxis   = list(title = "Estado"),
    yaxis   = list(title = "Ocorrências"),
    barmode = "group",
    legend  = list(orientation = "v",
                   x = 1.02, xanchor = "left")
  )

Orange

library(plotly)

data(Orange)

cores_arv <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd")

fig_o <- plot_ly()

for (tree_id in as.character(1:5)) {
  d   <- Orange[as.character(Orange$Tree) == tree_id, ]
  idx <- as.integer(tree_id)

  fig_o <- add_trace(
    fig_o,
    data      = d,
    x         = ~age,
    y         = ~circumference,
    type      = "scatter",
    mode      = "lines+markers",
    name      = paste("Árvore", tree_id),
    line      = list(color = cores_arv[idx], width = 2),
    marker    = list(color = cores_arv[idx], size = 7),
    text      = ~paste0(
                  "Árvore ", tree_id,
                  " \u2014 idade: ", age,
                  " dias \u2014 circ.: ", circumference, " mm"
                ),
    hoverinfo = "text"
  )
}

fig_o |>
  layout(
    title  = "Crescimento das Laranjeiras",
    xaxis  = list(title = "Idade (dias)"),
    yaxis  = list(title = "Circunfer\u00eancia (mm)"),
    legend = list(title = list(text = "Árvore"))
  )