# All combinations for levelplot

October 7, 2011
By

(This article was first published on Quantum Forest » rblogs, and kindly contributed to R-bloggers)

In a previous post I explained how to create all possible combinations of the levels of two factors using `expand.grid()`. Another use for this function is to create a regular grid for two variables to create a levelplot or a contour plot.

For example, let’s say that we have fitted a multiple linear regression to predict wood stiffness (stiff, the response) using basic density (bd) and a measure of microfibril angle (t) as explanatory variables. The regression equation could be something like `stiffness = 3.439 + 0.009 bd - 0.052 t`. In our dataset bd had a range of 300 to 700 kg m-3, while t had a range from 50 to 70.

We will use the `levelplot()` function that is part of the `lattice` package of graphical functions, create a grid for both explanatory variables (every 10 for bd and every 1 for t), predict values of stiffness for all combinations of bd and t, and plot the results.

```library(lattice)

# Create grid of data
wood = expand.grid(bd = seq(300, 700, 10), t = seq(50, 70, 1))
wood\$stiffness = with(wood, 3.439 + 0.009*bd - 0.052*t)

levelplot(stiffness ~ bd*t, data = wood,
xlab='Basic density', ylab='T')
```

This code creates a graph like this. Simple.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , , ,