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()
ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +
stat_qq() +
stat_qq_line()
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
No hay comentarios:
Publicar un comentario