- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 407字
- 2021-08-05 17:30:28
Creating box plots
In this recipe, you will learn how to make box plots that are useful in comparing the spread of values in different measurements.
Getting ready
First, we need to load the metals.csv
example data file that contains measurements of metal concentrations in London's air (you can download this file from the code download section of the book's companion website):
metals<-read.csv("metals.csv",header=TRUE)
How to do it...
We can make a box plot to summarize the metal concentration data using the boxplot()
command as follows:
boxplot(metals, xlab="Metals", ylab="Atmospheric Concentration in ng per cubic metre", main="Atmospheric Metal Concentrations in London")

How it works...
The main argument, the boxplot()
function, takes a set of numeric values (in the form of a vector or data frame). In our first example, we used a dataset containing numerical values of air pollution data from London. The dark line inside the box for each metal represents the median of values for that metal. The bottom and top edges of the box represent the first and third quartiles, respectively. Thus, the length of the box is equal to the interquartile range (IQR, which is the difference between first and third quartiles). The maximum length of a whisker is a multiple of the IQR, (the default multiplier is approximately 1.5). The ends of the whiskers are at data points closest to the maximum length of the whisker.
All the points lying beyond these whiskers are considered outliers.
As with most other plot types, the common arguments such as xlab
, ylab
, and main
can be used to set the titles for the x and y axes and the graph itself, respectively.
There's more...
We can also make another type of box plot where we can group the observations by categories. For example, if we want to study the spread of copper concentrations by the source of the measurements, we can use a formula to include the source. First, we need to read the copper_site.csv
example data file, as follows:
copper<-read.csv("copper_site.csv",header=TRUE)
Then, we can add the following code:
boxplot(copper$Cu~copper$Source, xlab="Measurement Site", ylab="Atmospheric Concentration of Copper in ng per cubic metre", main="Atmospheric Copper Concentrations in London")

In this example, the boxplot()
function takes a formula as an argument. This formula, which is in the value~group
form (Cu~source
), specifies a column of values and the group of categories it should be summarized over.
See also
More detailed box plot recipes are presented in Chapter 8, Box and Whisker Plots.
- Instant uTorrent
- 基于Proteus和Keil的C51程序設(shè)計項目教程(第2版):理論、仿真、實踐相融合
- Effective STL中文版:50條有效使用STL的經(jīng)驗(雙色)
- 深入淺出SSD:固態(tài)存儲核心技術(shù)、原理與實戰(zhàn)(第2版)
- micro:bit魔法修煉之Mpython初體驗
- The Deep Learning with Keras Workshop
- 龍芯自主可信計算及應(yīng)用
- Istio服務(wù)網(wǎng)格技術(shù)解析與實踐
- 無蘋果不生活:OS X Mountain Lion 隨身寶典
- Hands-On Motion Graphics with Adobe After Effects CC
- 觸摸屏應(yīng)用技術(shù)從入門到精通
- 筆記本電腦的結(jié)構(gòu)、原理與維修
- Hands-On One-shot Learning with Python
- Practical Artificial Intelligence and Blockchain
- INSTANT Cinema 4D Starter