Gráficos com ggplot2

ggplot(dados): inicializa um objeto ggplot.

  1. geoms: define o tipo de mapa.

    • geom_line

    • geom_point

    • geom_bar

    • geom_boxplot

    • geom_smooth

    • geom_histogram

    • geom_density

    • geom_area

    • geom_abline

    • geom_tile

  2. aes(): define mapeamentos estéticos.

    • x e y

    • color

    • fill

    • shape

    • size

  3. labs(): define os rótulos dos eixos x e y, e do título.

  4. theme(): permite personalizar a aparência geral do gráfico, como cores, tamanhos de fonte, fundo e outros elementos visuais.

  5. facet_wrap() e facet_grid(): dividem o gráfico com base em uma ou duas variáveis categóricas, respectivamente.

  6. scale_XXXX(): Controla a escala dos eixos e de outras características visuais, como cores e tamanhos.

  7. coord_XXXX(): Define a coordenação do sistema de referência do gráfico.

library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(gapminder)
brazil <- gapminder[gapminder$country == "Brazil", ]

Gráfico de linha

ggplot(brazil) + 
  geom_line(mapping = aes(x = year, y = gdpPercap, color = "red")) +
  labs(title = "PIB per capita ao longo dos anos", x = "Ano", y = "PIB per capita")

Gráfico de pontos

ggplot(brazil) + 
  geom_point(mapping = aes(x = year, y = pop, color = lifeExp)) +
  labs(title = "População do Brasil ao longo dos anos", x = "Ano", y = "População")

ano <- subset(gapminder, year == 2007 & continent == "Americas")

Gráfico de barras

ggplot(ano, aes(x = pop, y = country, fill = lifeExp)) +
  geom_bar(stat = "identity", alpha = 0.5) +
  scale_fill_gradient(low = "black", high = "green") +
  labs(title = "População por País Americano em 2007", x = "População", y = "País")

Sobreposição de gráficos

ggplot(mtcars, aes(y = mpg, x = disp)) + 
  geom_point(shape = 5, size = 4, color = "green") +
  geom_smooth(method = "lm")
`geom_smooth()` using formula = 'y ~ x'

Múltiplos gráficos

ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 6) +
  facet_wrap(~ cyl)

Personalização da Legenda

head(PlantGrowth)
  weight group
1   4.17  ctrl
2   5.58  ctrl
3   5.18  ctrl
4   6.11  ctrl
5   4.50  ctrl
6   4.61  ctrl
bp <- ggplot(data = PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot()

bp

#Alterar a ordem dos itens da legenda
bp + scale_fill_discrete(breaks=c("trt1", "ctrl", "trt2"))

#Renomear a legenda
bp + labs(fill = "Exp")

# Tamanho do texto da legenda
bp + theme (legend.text = element_text(size = 12))       

# Remover o titulo da legenda
bp + theme(legend.title = element_blank())

#Remover a legenda
bp + guides(fill = FALSE)
Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
of ggplot2 3.3.4.

#Modificando textos
bp + scale_fill_discrete(name = "Experimento",
                         breaks = c("ctrl", "trt1", "trt2"),
                         labels = c("Control", "Treatment 1", "Treatment 2"))

#Modificando as cores
bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

#Modificando a posicao da legenda
bp + theme(legend.position="top")  #bottom, left, right

Personalização dos eixos

# Histograma 
ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, fill = "blue", color = "black", alpha = 0.8) +
  labs(x = "Milhas por Galão", y = "Frequência") +
  scale_x_continuous(breaks = seq(0, 40, by = 2), limits = c(10, 35)) +
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  theme_minimal()
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_bar()`).

Personalização do tema

# theme()

# Rótulos dos eixos 
# axis.text.x = element_text(angle = 45, hjust = 1)
# axis.text.y = element_text(angle = 0, hjust = 1)
  
# Tamanho do texto dos títulos dos eixos
# axis.title = element_text(size = 14)         
   
# Tamanho e alinhamento do título do gráfico
# plot.title = element_text(size = 16, hjust = 0.5)

# Linhas da grade: major = principal | minor = secundária
# panel.grid.major.x = element_line(color = "", linetype = "")
# panel.grid.minor.x = element_line(color = "", linetype = "")
# panel.grid.major.y = element_line(color = "", linetype = "")
# panel.grid.minor.y = element_line(color = "", linetype = "")
# linetype: solid(sólida), dashed(tracejada), dotted(pontilhada) 

# Remover todas as linhas da grade
# panel.grid = element_blank()  

# Remover linhas verticais da grade
# panel.grid.major.x = element_blank()  
# panel.grid.major.y = element_blank()  

# Remover linhas horizontais da grade
# panel.grid.minor.y = element_blank()
# panel.grid.minor.x = element_blank()

Gráfico de densidade

ggplot(mtcars, aes(x = hp)) +
  geom_density(fill = "skyblue", color = "darkblue", alpha = 0.7) +
  labs(x = "Potência em cavalos (hp)",
       y = "Densidade") +
  theme_minimal()

Gráfico de pizza

ggplot(mtcars, aes(x = "", fill = factor(cyl))) +
  geom_bar(width = 1, stat = "count") +
  geom_text(aes(label = ..count..), stat = "count", position = position_stack(vjust = 0.5)) +
  coord_polar("y") +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank()
  )
Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(count)` instead.

dados_2007 <- gapminder[gapminder$year == 2007, ]
soma_populacao_por_continente <- aggregate(pop ~ continent, data = dados_2007, sum)

ggplot(soma_populacao_por_continente, aes(x = "", y = pop, fill = continent)) +
  geom_bar(width = 1, alpha = 0.7, stat = "identity") +
  coord_polar("y") +
  labs(title = "Proporção da População Mundial por Continente em 2007") +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank()
  )

Gráfico para séries temporais

print(economics)
# A tibble: 574 × 6
   date         pce    pop psavert uempmed unemploy
   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
 1 1967-07-01  507. 198712    12.6     4.5     2944
 2 1967-08-01  510. 198911    12.6     4.7     2945
 3 1967-09-01  516. 199113    11.9     4.6     2958
 4 1967-10-01  512. 199311    12.9     4.9     3143
 5 1967-11-01  517. 199498    12.8     4.7     3066
 6 1967-12-01  525. 199657    11.8     4.8     3018
 7 1968-01-01  531. 199808    11.7     5.1     2878
 8 1968-02-01  534. 199920    12.3     4.5     3001
 9 1968-03-01  544. 200056    11.7     4.1     2877
10 1968-04-01  544  200208    12.3     4.6     2709
# ℹ 564 more rows
#Variações percentuais para a coluna psavert 
economics$returns_perc <- c(0, diff(economics$psavert)/economics$psavert[-length(economics$psavert)])


ggplot(economics, aes(x=date)) + 
  geom_line(aes(y=returns_perc)) + 
  labs(title="Variação dos retornos percentuais ao longo do tempo", 
       caption="Source: Economics", 
       y="Returns %")

##Por mês
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
economics_m <- economics[1:24, ]

lbls <- paste0(month.abb[month(economics_m$date)], " ", lubridate::year(economics_m$date))
brks <- economics_m$date

ggplot(economics_m, aes(x=date)) + 
  geom_line(aes(y=returns_perc), color = "red") + 
  labs(title="Série temporal por mês das 24 primeiras observações", 
       caption="Source: Economics", 
       y="Returns %") +  
  scale_x_date(labels = lbls, 
               breaks = brks) +  
  theme(axis.text.x = element_text(angle = 90, vjust=0.5),  
        panel.grid.minor = element_blank())  

##Por ano
economics_y <- economics[1:90, ]

brks <- economics_y$date[seq(1, length(economics_y$date), 12)]
lbls <- lubridate::year(brks)

ggplot(economics_y, aes(x=date)) + 
  geom_line(aes(y=returns_perc), color = "green") + 
  labs(title="Série temporal por ano das 90 primeiras observações", 
       y="Returns %") + 
  scale_x_date(labels = lbls, 
               breaks = brks) +  
  theme(axis.text.x = element_text(angle = 90, vjust=0.5),  
        panel.grid.minor = element_blank())  

Gráfico sazonal

library(forecast)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
head(AirPassengers)
     Jan Feb Mar Apr May Jun
1949 112 118 132 129 121 135
ggseasonplot(AirPassengers) + labs(title="International Airline Passengers")

head(nottem)
      Jan  Feb  Mar  Apr  May  Jun
1920 40.6 40.8 44.4 46.7 54.1 58.5
subset_nottem <- window(nottem, start=c(1920, 1), end=c(1925, 12))

ggseasonplot(subset_nottem) + labs(title="Air temperatures at Nottingham Castle")

Heatmap

head(airquality)
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
ggplot(airquality, aes(x = Month, y = Day, fill = Temp)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "blue", high = "red") +
  labs(x = "Mês",
       y = "Dia",
       fill = "Temperatura") +
  theme_minimal()

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Sepal.Width)) +
  geom_tile(color = "black") +
  geom_text(aes(label = Sepal.Width), color = "white", size = 2) +
  coord_fixed()  +
  labs(x = "Espécie",
       y = "Comprimento da Sépala",
       fill = "Largura da Sépala") +
  theme_minimal()