2019-04-25

Plot median and quartiles in ggplot2 using geom_pointrange

Problem

We would like to plot the interquartile range (IQR) and the median in ggplot2.

Solution

In this case we use the function geom_pointrange. We need to specify the interval (interquartile range) with the arguments fun.ymin and fun.ymax, and fun.y to plot the median.

library(ggplot2)
ggplot(data = diamonds) +
  geom_pointrange(mapping = aes(x = cut, y = depth),
                  stat = "summary",
                  fun.ymin = function(z) {quantile(z,0.25)},
                  fun.ymax = function(z) {quantile(z,0.75)},
                  fun.y = median)

Results

Related posts

Spanish version

References

2019-04-08

Quantile-quantile (Q-Q) plots with ggplot2

Problem

We'd like to create quantile-quantile (Q-Q) plots using ggplot2.

Solution

  • Option 1: one column.
  • We create two examples using one column of a data frame. In the first example, we previously transform the vector rivers into a data frame. In the second, we use the column Volume from the data frame trees.

    library(tidyverse)
    ggplot(data.frame(rivers), aes(sample = rivers)) + stat_qq() + stat_qq_line()
    ggplot(trees, aes(sample = Volume)) + stat_qq() + stat_qq_line()
    
  • Option 2: multiple columns.
  • We compare the distribution of Miles/(US) gallon by number of cylinders (cyl).

    ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +
      stat_qq() +
      stat_qq_line()
    
  • Option 3: multiple panels.
  • In this first example no transformation is needed because a column of the data frame contains the groups..

    ggplot(mtcars, aes(sample = mpg)) +
      facet_wrap( ~ factor(cyl)) +
      stat_qq() +
      stat_qq_line()
    
    In the second example, we convert the data frame from wide format to long format for faceting into multiple panels with facet_wrap. Instead of 31 observations for 3 variables, we will have one column condition containing 93 observaciones for the 3 variables.

    gather(trees, condition, measurement, Girth:Volume, factor_key = TRUE) %>%
      ggplot(aes(sample = measurement)) +
      facet_wrap( ~ condition, scales = "free") +
      stat_qq() +
      stat_qq_line()
    
    In the third example, we convert from wide to long, facet by chemical composition, and use a colour for each kiln.

    library(HSAUR2)
    gather(pottery, condition, measurement, Al2O3:BaO, factor_key = TRUE) %>%
      ggplot(aes(sample = measurement, colour = kiln)) +
      facet_wrap(~ condition, scales = "free") +
      stat_qq() +
      stat_qq_line()
    

References

Nube de datos