VADeaths

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
cores <- c("#E63946", "#F4A261", "#2A9D8F", "#457B9D", "#6A0572")

barplot(
  VADeaths,
  beside    = TRUE,
  col       = cores,
  main      = "Taxa de Mortalidade na Virginia (1940)",
  xlab      = "Grupo Populacional",
  ylab      = "Taxa de Mortalidade (por 1000 habitantes)",
  ylim      = c(0, max(VADeaths) * 1.3),
  las       = 1,
  border    = NA
)

legend(
  "topleft",
  legend = rownames(VADeaths),
  fill   = cores,
  title  = "Faixa Etaria",
  border = NA,
  bty    = "n"
)

ClassificaçãoDoença

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

contagem <- table(pacientes)

contagem <- contagem[c("leve", "moderado", "severo")]

pct <- round(100 * contagem / sum(contagem), 1)

rotulos <- paste0(pct, "%")

cores_pizza <- c("#2A9D8F", "#F4A261", "#E63946")

pie(
  contagem,
  labels = rotulos,
  col    = cores_pizza,
  main   = "Classificacao de Doenca por Estagio (20 pacientes)",
  border = "white"
)

legend(
  "bottomright",
  legend = c("Leve", "Moderado", "Severo"),
  fill   = cores_pizza,
  title  = "Estagio",
  border = NA,
  bty    = "n"
)

USArrests

library(plotly)

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

top5 <- head(dados[order(dados$Murder, decreasing = TRUE), ], 5)

fig <- plot_ly(
  data = top5,
  x    = ~City,
  y    = ~Murder,
  type = "bar",
  name = "Murder",
  marker = list(color = "#E63946")
) %>%
  add_trace(
    y    = ~Rape,
    name = "Rape",
    marker = list(color = "#F4A261")
  ) %>%
  add_trace(
    y    = ~Assault,
    name = "Assault",
    marker = list(color = "#457B9D")
  ) %>%
  layout(
    barmode = "group",
    title   = "Top 5 Estados com Maior Taxa de Homicidio (EUA)",
    xaxis   = list(title = "Estado"),
    yaxis   = list(title = "Taxa de Criminalidade (por 100.000 hab.)"),
    legend  = list(title = list(text = "<b>Tipo de Crime</b>"))
  )

fig

Orange

library(plotly)

cores_arvore <- c("#E63946", "#F4A261", "#2A9D8F", "#457B9D", "#6A0572")

fig_orange <- plot_ly()

for (i in 1:5) {
  sub <- subset(Orange, Tree == i)

  fig_orange <- add_trace(
    fig_orange,
    data       = sub,
    x          = ~age,
    y          = ~circumference,
    type       = "scatter",
    mode       = "lines+markers",
    name       = paste0("Arvore ", i),
    line       = list(color = cores_arvore[i], width = 2),
    marker     = list(color = cores_arvore[i], size = 7),
    text       = ~paste0("Arvore ", i,
                         " \u2014 idade: ", age, " dias",
                         " \u2014 circ.: ", circumference, " mm"),
    hoverinfo  = "text"
  )
}

fig_orange <- layout(
  fig_orange,
  title  = "Crescimento das Laranjeiras ao Longo do Tempo",
  xaxis  = list(title = "Idade (dias)"),
  yaxis  = list(title = "Circunferencia (mm)"),
  legend = list(title = list(text = "<b>Laranjeira</b>"))
)

fig_orange