Carregar pacotes ggplot2 e MASS e carregar dataframe Cars93.

library(ggplot2)
library(MASS)
View(Cars93)

Diagrama de Caixa

Mostrar como os grupos diferem uns dos outros.

ggplot(Cars93, aes(x= Cylinders, y= Horsepower)) + geom_boxplot()

Vamos mostrar todas as marcas de dados além das caixas adicionando geom_point:

ggplot(Cars93, aes(x= Cylinders, y= Horsepower)) + geom_boxplot() + geom_point()

São dados de 93 carros. Muitos dados se sobrepõem, por isso não vemos todos. Nome disso é overplotting.

Mas podemos lidar com os overplottings reposicionando as marcas para que apareçam todas, isso se chama jittering, com a função geom_jitter:

ggplot(Cars93, aes(x= Cylinders, y= Horsepower)) + geom_boxplot() + geom_point() + geom_jitter()

Exemplos extras

library(ggplot2)
theme_set(theme_classic())

# Plot
g <- ggplot(mpg, aes(class, cty))
g + geom_boxplot(varwidth=T, fill="plum") + 
    labs(title="Box plot", 
         subtitle="City Mileage grouped by Class of vehicle",
         caption="Source: mpg",
         x="Class of Vehicle",
         y="City Mileage")

library(ggthemes)
g <- ggplot(mpg, aes(class, cty))
g + geom_boxplot(aes(fill=factor(cyl))) + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) + 
  labs(title="Box plot", 
       subtitle="City Mileage grouped by Class of vehicle",
       caption="Source: mpg",
       x="Class of Vehicle",
       y="City Mileage")

Dot + Box Plot

library(ggplot2)
theme_set(theme_bw())

# plot
g <- ggplot(mpg, aes(manufacturer, cty))
g + geom_boxplot() + 
  geom_dotplot(binaxis='y', 
               stackdir='center', 
               dotsize = .5, 
               fill="red") +
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) + 
  labs(title="Box plot + Dot plot", 
       subtitle="City Mileage vs Class: Each dot represents 1 row in source data",
       caption="Source: mpg",
       x="Class of Vehicle",
       y="City Mileage")
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

Referências

Schmuller, Joseph. Análise Estatística com R - Para leigos - Tradução da 2a edição. Alta Books Editora. 2019.

Oliveira, Paulo; Guerra, Saulo; McDonnell, Robert. Ciência de Dados com R. Introdução.Editora IBPAD. Brasília. 2018. Disponível em: {https://cdr.ibpad.com.br/cdr-intro.pdf}