Half of boxplot appearing in ggplot2 function

Asked

Viewed 45 times

1

I’m doing a boxplot using ggplot2 package, however, for some external reason, only half of the boxplot is being made for the "Control" and "Commercial IMD" treatments".

See below that when doing the graph using the "boxplot" function, the graph is usually done.

mediasCon = tapply(dados$CS, dados$Trat, mean)
boxplot(dados$CS ~ dados$Trat, data = dados, col="gray", 
        xlab = 'Tratamentos', ylab = 'Espermatozoides - Cabeça Solta')
points(1:3, mediasCon, col = 'Red', pch = 16)

However, when making the same graph using the GGPLOT2 function, see that for the first two treatments only half of the graph is being made, why this is occurring?

Also, as I add "tails" to boxplot using ggplot2 function?

library(ggplot2)
ggplot(data=dados, aes(x=Trat, y=CS)) + geom_boxplot(fill=c("#DEEBF7","#2171B5","#034E7B"),color="black") +
  xlab('Tratamentos') +
  ylab('Espermatozoides - Cabeça Solta') + 
  stat_summary(fun=mean, colour="black", geom="point", 
               shape=18, size=5) + 
                     theme(axis.title = element_text(size = 20),
                     axis.text = element_text(size = 16)) 

1 answer

2


The boxplots are not appearing in half. The data has a very pronounced asymmetry, which makes some statistics get confused. For example, the CS variable has the minimum and first quartile identical for the control, while its third quartile and maximum are equal for the Commercial IMD.

library(tidyverse)

dados %>% 
  split(.$Trat) %>% 
  map(summary)
## $Controle
##      Trat                Rep             CS            FD             FS      
##  Length:4           Min.   :1.00   Min.   :0.0   Min.   :0.00   Min.   :0.00  
##  Class :character   1st Qu.:1.75   1st Qu.:0.0   1st Qu.:0.00   1st Qu.:0.75  
##  Mode  :character   Median :2.50   Median :0.0   Median :0.50   Median :2.00  
##                     Mean   :2.50   Mean   :0.5   Mean   :0.75   Mean   :2.00  
##                     3rd Qu.:3.25   3rd Qu.:0.5   3rd Qu.:1.25   3rd Qu.:3.25  
##                     Max.   :4.00   Max.   :2.0   Max.   :2.00   Max.   :4.00  
##        FE       
##  Min.   : 9.00  
##  1st Qu.: 9.75  
##  Median :10.50  
##  Mean   :11.25  
##  3rd Qu.:12.00  
##  Max.   :15.00  
## 
## $`IMD Comercial`
##      Trat                Rep             CS             FD            FS     
##  Length:4           Min.   :1.00   Min.   :0.00   Min.   :0.0   Min.   :2.0  
##  Class :character   1st Qu.:1.75   1st Qu.:0.75   1st Qu.:0.0   1st Qu.:3.5  
##  Mode  :character   Median :2.50   Median :1.00   Median :1.5   Median :5.0  
##                     Mean   :2.50   Mean   :0.75   Mean   :1.5   Mean   :4.5  
##                     3rd Qu.:3.25   3rd Qu.:1.00   3rd Qu.:3.0   3rd Qu.:6.0  
##                     Max.   :4.00   Max.   :1.00   Max.   :3.0   Max.   :6.0  
##        FE       
##  Min.   :14.00  
##  1st Qu.:14.00  
##  Median :16.00  
##  Mean   :16.25  
##  3rd Qu.:18.25  
##  Max.   :19.00  
## 
## $`IMD Princ\xedpio Ativo`
##      Trat                Rep             CS            FD            FS    
##  Length:4           Min.   :1.00   Min.   :1.0   Min.   :1.0   Min.   : 1  
##  Class :character   1st Qu.:1.75   1st Qu.:1.0   1st Qu.:1.0   1st Qu.: 1  
##  Mode  :character   Median :2.50   Median :1.5   Median :2.5   Median : 6  
##                     Mean   :2.50   Mean   :1.5   Mean   :2.5   Mean   : 6  
##                     3rd Qu.:3.25   3rd Qu.:2.0   3rd Qu.:4.0   3rd Qu.:11  
##                     Max.   :4.00   Max.   :2.0   Max.   :4.0   Max.   :11  
##        FE      
##  Min.   : 9.0  
##  1st Qu.: 9.0  
##  Median :13.5  
##  Mean   :13.5  
##  3rd Qu.:18.0  
##  Max.   :18.0
  • Thank you! But because in the "boxplot" function the graph is dissimilar to the one executed by the "ggplot" function"?

  • 2

    @Brenog. the documentation of geom_boxplot says that the calculation for the ends of the boxplot is slightly different from the function of the base r and that this can become apparent in small data sets, as seems to be the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.