QQ plots: comparing multiple variables to each other in a single figure

42 Views Asked by At

I want to make a matrix of QQ plots like the one posted hereenter image description here [Sample set QQ plot comparing different elements to each other] . Below is a condensed version of my data frame. For each scan (a-c), I want a QQ plot to compare the grain size (numerical values below the element names) for each chemical element (s, fe, cr, zr) to each other. Because I have many scans, I will need to make this matrix of QQ plots multiple times. I am pretty new to R so specific code to help me make these plots would be appreciated. Thank you

# Packages
library(tidyverse)
library(ggplot2)
library(dplyr)
library(GGally)
print(df, n = 27)

   scan  grain     s    fe    cr    zr
 1 a         1    22    21    13    25
 2 a         2    33    31    23    35
 3 a         3    44    41    45    45
 4 a         4    55    51    56    56
 5 a         5    66    61    67    65
 6 a         6    66    71    78    75
 7 a         7    77    81    89    85
 8 a         8    88    82    97    95
 9 a         9    92    93    72    97
10 b         1    23    22    14    26
11 b         2    34    32    24    36
12 b         3    45    42    46    46
13 b         4    56    52    57    57
14 b         5    67    62    68    66
15 b         6    67    72    79    76
16 b         7    78    82    90    86
17 b         8    89    83    98    96
18 b         9    93    94    73    98
19 c         1    24    23    15    27
20 c         2    35    33    25    37
21 c         3    46    43    47    47
22 c         4    57    53    58    58
23 c         5    68    63    69    67
24 c         6    68    73    80    77
25 c         7    79    83    91    87
26 c         8    90    84    99    97
27 c         9    94    95    74    99

I have used

ggplot( aes(sample = diameter, color = element, shape = element, )) + 
  stat_qq() + 
  stat_qq_line() +  
  facet_wrap(~ scan, nrow = 3) 

to compare each element to the theoretical quantile, but not to each other element.

I tried using GGally with ggpairs(df, columns = 3:6) (https://i.stack.imgur.com/wH1mt.png) This made a matrix, but I can’t figure out how to make each plot a QQ plot

ggpairs figure comparing elements, but not as a  QQ plot.

1

There are 1 best solutions below

1
Rui Barradas On

Are you looking for something like this?
If yes, this type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

With the data in long format, map value to the sample aesthetic then plot by name (with facet_wrap).

library(ggplot2)

df1 |>
  tidyr::pivot_longer(s:zr) |>
  ggplot(aes(sample = value)) +
  geom_qq() +
  geom_qq_line() +
  facet_wrap(~ name)

Created on 2024-03-29 with reprex v2.1.0


Data

df1 <- 
  structure(list(
    scan = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", 
             "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c"), 
    grain = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
              7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), 
    s = c(22L, 33L, 44L, 55L, 66L, 66L, 77L, 88L, 92L, 23L, 34L, 45L, 56L, 
          67L, 67L, 78L, 89L, 93L, 24L, 35L, 46L, 57L, 68L, 68L, 79L, 90L, 94L), 
    fe = c(21L, 31L, 41L, 51L, 61L, 71L, 81L, 82L, 93L, 22L, 32L, 42L, 
           52L, 62L, 72L, 82L, 83L, 94L, 23L, 33L, 43L, 53L, 63L, 73L, 83L, 84L, 95L), 
    cr = c(13L, 23L, 45L, 56L, 67L, 78L, 89L, 97L, 72L, 14L, 24L, 46L, 57L, 68L, 
           79L, 90L, 98L, 73L, 15L, 25L, 47L, 58L, 69L, 80L, 91L, 99L, 74L), 
    zr = c(25L, 35L, 45L, 56L, 65L, 75L, 85L, 95L, 97L, 26L, 36L, 46L, 57L, 66L, 
           76L, 86L, 96L, 98L, 27L, 37L, 47L, 58L, 67L, 77L, 87L, 97L, 99L)), 
    class = "data.frame", 
    row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
                  "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 
                  "23", "24", "25", "26", "27"))