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

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

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