Plotting Composite Material Data

Ally Fraser

2-May-2020

This vignette demonstrates how to create some of the graphs commonly used when analyzing composite material data. Here, we rely on the ggplot2 package for graphing. This package can be loaded either on its own, or through the tidyverse meta-package, which also includes packages such as dplyr that we will also use.

We’ll need to load a few packages in order to proceed.

library(dplyr)
library(ggplot2)
library(tidyr)
library(cmstatr)

Throughout this vignette, we’ll use one of the example data sets that comes with cmstatr and we’ll focus on the warp-tension data as an example. We’ll load the example data in a variable as follows. By default the condition will be in an arbitrary order, but throughout the visualization, we’ll want the conditions shown in a particular order (from coldest and driest to hottest and wettest). We can define the order of the conditions using the ordered function. For brevity, only the first few rows of the data set are displayed below.

dat <- carbon.fabric.2 %>%
  filter(test == "WT") %>%
  mutate(condition = ordered(condition, c("CTD", "RTD", "ETW", "ETW2")))

dat %>%
  head(10)
#>    test condition batch panel thickness nplies strength modulus failure_mode
#> 1    WT       CTD     A     1     0.112     14  142.817   9.285          LAT
#> 2    WT       CTD     A     1     0.113     14  135.901   9.133          LAT
#> 3    WT       CTD     A     1     0.113     14  132.511   9.253          LAT
#> 4    WT       CTD     A     2     0.112     14  135.586   9.150          LAB
#> 5    WT       CTD     A     2     0.113     14  125.145   9.270          LAB
#> 6    WT       CTD     A     2     0.113     14  135.203   9.189          LGM
#> 7    WT       CTD     A     2     0.113     14  128.547   9.088          LAB
#> 8    WT       CTD     B     1     0.113     14  127.709   9.199          LGM
#> 9    WT       CTD     B     1     0.113     14  127.074   9.058          LGM
#> 10   WT       CTD     B     1     0.114     14  126.879   9.306          LGM

We’ll then calculate the B-Basis value using the pooling by standard deviation method. This data set happens to fail some of the diagnostic tests, but for the purpose of this example, we’ll ignore those failures using the override argument.

b_basis_pooled <- dat %>%
  basis_pooled_cv(strength, condition, batch,
                  override = c("between_group_variability",
                               "normalized_variance_equal"))

b_basis_pooled
#> 
#> Call:
#> basis_pooled_cv(data = ., x = strength, groups = condition, batch = batch, 
#>     override = c("between_group_variability", "normalized_variance_equal"))
#> 
#> Distribution:  Normal - Pooled CV    ( n = 86, r = 4 )
#> The following diagnostic tests were overridden:
#>     `between_group_variability`,
#>     `normalized_variance_equal`
#> B-Basis:   ( p = 0.9 , conf = 0.95 )
#> CTD   125.1325 
#> RTD   129.3447 
#> ETW   123.809 
#> ETW2  120.3191

The object returned from basis_pooled_cv contains a number of values. One value is a data.frame containing the groups (i.e. conditions) and the corresponding basis values. This looks like the following. We’ll use this in the visualizations.

b_basis_pooled$basis
#>      group    value
#> CTD    CTD 125.1325
#> RTD    RTD 129.3447
#> ETW    ETW 123.8090
#> ETW2  ETW2 120.3191

Batch Plots

Batch plots are used to identify differences between batches. Simple batch plots can be created using box plots and adding horizontal lines for the basis values as follows. Note that the heavy line in the box of the box plot is the median, not the mean. The two hinges correspond with the first and third quantiles and the whiskers extend to the most extreme data point, or 1.5 times the inner quantile range.

In the code below, we use the function rename to rename the column group to condition. The data.frame produced by basis_pooled_cv uses the columns value and group, but to match the data, we need the column with the conditions to be named condition.

dat %>%
  ggplot(aes(x = batch, y = strength)) +
  geom_boxplot() +
  geom_jitter(width = 0.25) +
  geom_hline(aes(yintercept = value),
             data = b_basis_pooled$basis %>% rename(condition = group),
             color = "blue") +
  facet_grid(. ~ condition) +
  theme_bw() +
  ggtitle("Batch Plot")

It’s sometimes useful add failure modes to the plot so that you can easily identify whether failure modes differ between conditions or batches. This can be done with the geom_jitter_failure_mode() function. This behaves very similarly to geom_jitter(), except that color or shape must be specified, and if there are multiple failure modes (e.g. “LAB/LAT”), both failure modes are plotted as separate points.

dat %>%
  ggplot(aes(x = batch, y = strength)) +
  geom_boxplot() +
  geom_jitter_failure_mode(aes(color = failure_mode, shape = failure_mode), width = 0.25) +
  geom_hline(aes(yintercept = value),
             data = b_basis_pooled$basis %>% rename(condition = group),
             color = "blue") +
  facet_grid(. ~ condition) +
  theme_bw() +
  ggtitle("Batch Plot with Failure Modes")