Experimental analysis, how to create a contour plot in R

What is a contour plot?

A contour plot is commonly used in multivariate analysis. In the context of experimental analysis it can be used to find the optimal combination of multiple different factors.

Example dataset, Bob has 3 different material types (A) and he wants to see which one of them can survive in the coldest temperature celsius. Bob has also created a magic mixture that he applies on each material type in hope for some cold resistance (B), in addition there are 3 different sizes (C) on the material types. So for this experiment there are 3 factors and he wants to find the combination of A, B and C that will yield the most cold resistance.

Material Type (A)
1 2 3
Mg of magic mixture (B)
Size
(C)
100 120 140 100 120 140 100 120 140
10 -35 -45 -40 17 -65 20 -39 -55 15
-25 -60 15 24 -58 4 -35 -67 -30
15 110 -10 80 55 -55 110 90 -28 110
75 30 54 120 -44 44 113 -26 135
20 4 -40 31 -23 -64 -20 -30 -61 54
5 -30 36 -5 -62 -31 -55 -52 4

Experimental analysis, how to create a contour plot in R


#First we want to get the data into R, for this example we manually input it. For larger datasets it is recommend to use the read.csv function ie dataset <- read.csv(file = 'data/dataset.csv')

#Manually input dataset
y <- c(-35, -25, 110, 75, 4, 5, -45, -60, -10, 30, -40, -30, -40, 15, 80, 54, 31, 36, 17, 24, 55, 120, -23, -5, -65, -58, -55, -44, -64, -62, 20, 4, 110, 44, -20, -31, -39, -35, 90, 113, -30, -55, -55, -67, -28, -26, -61, -52, 15, -30, 110, 135, 54, 4)

A1 <- y[1:18] #Column 1, 2 & 3 for material type 1

A2 <- y[19:36] #Column 4, 5 & 6 for material type 2

A3 <- y[37:54] #Column 7, 8 & 9 for material type 3


B <- rep(c(-1, 0, 1), each = 6, times = 1) #Code each level for B: -1, 0, 1. 6 rows in each column.

C <- rep(c(-1, 0, 1), each = 2, times = 3) #Code each level for C: -1, 0, 1. Two rows each level per column.

#Regression model
lm.A1 <- lm(A1 ~ B + C + I(B^2) + I(C^2) + B:C) #Regression model to find best possible combinations (material A1)
lm.A2 <- lm(A2 ~ B + C + I(B^2) + I(C^2) + B:C) #Regression model to find best possible combinations (material A2)
lm.A3 <- lm(A3 ~ B + C + I(B^2) + I(C^2) + B:C) #Regression model to find best possible combinations (material A3)

tmp.B <- seq(-1, 1, by = 0.01) #Coordinates for the contour plot, try around and find values that fit your dataset the best.

tmp.C <- seq(-1, 1, by = 0.01) #Coordinates for the contour plot, try around and find values that fit your dataset the best.

#Create a table with coordinates and their corresponding fitted values
tmp <- list(B = tmp.B, C = tmp.C)
new <- expand.grid(tmp) #Table with fitted values
new$fit.1 <- predict(lm.A1, new) #Add predicted values to the table (plot for material A1)
new$fit.2 <- predict(lm.A2, new) #Add predicted values to the table (plot for material A2)
new$fit.3 <- predict(lm.A3, new) #Add predicted values to the table (plot for material A3)

library(lattice) #Used to generate the plot

#Generate the contour plots
contourplot(fit.1 ~ B*C, data = new, cuts = 8, xlab = "mg of magic mixture", ylab = "Size", main = "Material type 1 (A = -1)") #plot for material type (A1)

contourplot(fit.2 ~ B*C, data = new, cuts = 10, xlab = "mg of magic mixture", ylab = "Size", main = "Material type 2 (A = 0)") #plot for material type (A2)

contourplot(fit.3 ~ B*C, data = new, cuts = 10, xlab = "mg of magic mixture", ylab = "Size", main = "Material type 3 (A = 1)") #plot for material type (A3)

R output


contoura11


contoura22


contoura33

Since Bob wants to find the material with the most cold resistance he now looks for the lowest value in all the plots. In this case the lowest value can be seen in material type 3 plot at -80 degrees celsius. The best combination is material type 3 with medium level of magic mixture and the lowest size level.