Eixos - Escalas - Cores

Author

Ricardo Accioly

Published

August 20, 2024

Carregando bibliotecas

Filtrando dados e fazendo grafico de dispersão padrão

gap_07 <- filter(gapminder, year == 2007)
ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Transformando o eixo x para escala logarítimica

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  scale_x_continuous(trans = "log10")

Outra forma de transformação do eixo x

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  scale_x_log10()

Definindo limites para o eixo y

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  scale_x_log10() +
  scale_y_continuous(limits = c(0, 95))

Grafico com cores normais

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10()

Grafico usando uma paleta de cores diferente

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10() +
  scale_color_brewer(palette = "Dark2")

Usando codigos manuais para as cores

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10() +
  scale_color_manual(values = c("#FF0000", "#00A08A", "#F2AD00",
                                "#F98400", "#5BBCD6"))

Definindo as cores e tamanho dos pontos

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp)) +
  geom_point(color = "darkblue", size = 3) +
  scale_x_log10()

Customizando títulos, rótulos de eixo e legendas

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(size = 2) +
  scale_x_log10() +
  theme_light() +
  theme(legend.position = "bottom")

Sem legenda

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(size = 2) +
  scale_x_log10() +
  theme_light() +
  theme(legend.position = "none")

Legenda dentro do gráfico

ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(size = 2) +
  scale_x_log10() +
  theme_light() +
  theme(legend.position = c(0.1, 0.85))
#> Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2
#> 3.5.0.
#> ℹ Please use the `legend.position.inside` argument of `theme()` instead.

Aumentando o tamanho do texto e mudando para portugues

graf2 <- ggplot(gap_07, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(size = 2) +
  scale_x_log10() +
  theme_light() +
  theme(legend.position = c(0.1, 0.85),
        legend.key = element_blank(),
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 14)) +
  labs(x = "PIB Per capita (US$)", 
       y = "Expectativa de Vida (anos)", 
       title = "Expectativa de Vida vs PIB em 2007",
       color = "Continente")

graf2