Mostrando entradas con la etiqueta boxplot. Mostrar todas las entradas
Mostrando entradas con la etiqueta boxplot. Mostrar todas las entradas

2020-12-11

How to modify whiskers of a boxplot in ggplot2?

Title

Problem

We want to modify the colour of the whiskers in a boxplot using ggplot2. In our example, the default colour (black) to red.

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am)))
p + geom_boxplot()

Solution

  • Option 1
  • We plot error bars in red and then superimpose the boxplots in black.

    library(ggplot)
    p + stat_boxplot(
      geom = "errorbar",
      colour = "red",
      width = 0,
      position = position_dodge(0.75)
    ) +
      geom_boxplot(coef = 0, outlier.shape = NA)
    
  • Option 2
  • We superimpose two boxplots on top of each other. The first one with red borders and the secong one without whiskers in black.

    p + geom_boxplot(color="red") + 
      geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) 
    

References

2020-12-04

How to remove whiskers in a boxplot in ggplot2

Title

Problem

We want to remove whiskers in a boxplot created with ggplot2.

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()

Solution

We specify coef = 0, overwriting the default value (coef = 1.5).

p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(outlier.size = 0, coef = 0)

Related posts

2020-12-03

How to remove whiskers in a boxplot with the R Base Package

Title

Problem

We want to remove whiskers in a boxplot created with the R base package.

boxplot(mpg ~ cyl, data = mtcars)

Solution

We pass the arguments: whisklty = 0, staplelty = 0.

whisklty - to control the whisker line type (default: "dashed").
staplelty - to control the staple (= end of whisker) line type.

boxplot(mpg ~ cyl, data = mtcars, whisklty = 0, staplelty = 0)

Related posts

2020-11-20

How to change the width of error bars in ggplot2

Problem

We want to change the width of the error bars in a boxplot created with ggplot2. We use stat_boxplot(geom ='errorbar') to add error bars.

library(ggplot2)
ggplot(iris, aes(factor(Species), Sepal.Width, fill = Species)) +
    geom_boxplot() + 
    stat_boxplot(geom ='errorbar')

Solution

We use the argument width inside stat_boxplot.

library(ggplot2)
ggplot(iris, aes(factor(Species), Sepal.Width, fill = Species)) +
  geom_boxplot() + 
  stat_boxplot(geom ='errorbar', width = 0.5) 

2019-09-13

How to annotate a boxplot in ggplot2

Problem

We want to annotate a boxplot in ggplot2. In our example we will add Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, and maximum).

fivenum(iris$Sepal.Width)
[1] 2.0 2.8 3.0 3.3 4.4
library(ggplot2)
ggplot(iris, aes(factor(0), Sepal.Width)) + 
geom_boxplot() + xlab("") + scale_x_discrete(breaks = NULL)

Solution

We store Tukey's five number summary as a data frame, and then pass it as the argument data inside geom_text to add text to the plot.

five <- data.frame(x = rep(1, 5), five = fivenum(iris$Sepal.Width))
ggplot(iris, aes(x = 0, y = Sepal.Width)) + 
  geom_boxplot() + xlab("") + scale_x_discrete(breaks = NULL) + 
  geom_text(data = five, aes(x = 0, y = five, label = five), nudge_x = 0.5)

References

2019-05-11

How to create boxplots grouping into intervals x-axis values using R?

Problem

We want to create boxplots grouping into intervals the values of the x-axis, like in the example below.

Solution

Data

We generate random data and a sequence to create the intervals.

set.seed(12)
y <- rnorm(1000)
x <- rnorm(1000)
rng <- seq(-3, 3, 0.5)
  • Base
  • boxplot(y ~ cut(x, breaks = rng),las=2)
    
    If we'd like to include not available data (NAs), we use the function addNA:

    boxplot(y ~ addNA(cut(x, breaks = rng)), las = 2)
    
  • ggplot2
  • First we create data frame with the intervals.

    library(ggplot2)
    df <- data.frame(x = cut(x, breaks = rng), y = y)
    ggplot(data = df, aes(x = x, y = y)) + geom_boxplot(aes(fill = x))
    

    Related posts

    References

    2017-07-29

    Anotar diagrama de caja en ggplot2

    Problema

    Deseamos anotar el siguiente diagrama de caja en ggplot2 y añadir los 5 números de Tukey: mínimo, bigote inferior, mediana, bigote superior, máximo.

    fivenum(iris$Sepal.Width)
    
    [1] 2.0 2.8 3.0 3.3 4.4
    
    library(ggplot2)
    ggplot(iris, aes(factor(0), Sepal.Width)) + 
    geom_boxplot() + xlab("") + scale_x_discrete(breaks = NULL)
    

    Solución

    Creamos el data frame five con los 5 números de Tukey que utilizamos en los argumentos de geom_text para anotar el gráfico.

    five <- data.frame(x = rep(1, 5), five = fivenum(iris$Sepal.Width))
    ggplot(iris, aes(x = 0, y = Sepal.Width)) + 
      geom_boxplot() + xlab("") + scale_x_discrete(breaks = NULL) + 
      geom_text(data = five, aes(x = 0, y = five, label = five), nudge_x = 0.5)
    

    Referencias

    Entradas relacionadas

    Nube de datos