Grouping over a variable

In this recipe, we will see how we can summarize data for a variable with respect to another variable in the dataset. We will learn to group over a variable such that a separate box plot is created for each group.

Getting ready

We will only use the base graphics functions for this recipe. So, just open up the R prompt and type in the following code. We will use the metals.csv example dataset for this recipe. So, let's first load it:

metals<-read.csv("metals.csv")

How to do it...

Let's make a box plot that shows copper (Cu) concentrations grouped over measurement sites:

boxplot(Cu~Source,data=metals,
main="Summary of Copper (Cu) concentrations by Site")

How it works...

The preceding box plot works by using the formula notation, ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.