Use the boxplot function to draw a box plot in R language

(From Baidu, copied here for easy viewing, if there is any offense, it will be deleted)

Introduction to Boxplots

The box plot, also known as the box plot or the box-whisker plot, is a graph drawn from five eigenvalues.

The 5 eigenvalues ​​are the maximum, minimum, median, 1st quartile, and 3rd quartile of the variable.

Connect the two quantiles to draw a box, divide the box by the median, and connect the two extreme points with the box to form a box plot.

Function boxplot for drawing box plots in R

(1) Basic usage

boxplot(x, ...)

(2) Usage of formula form

boxplot(formula, data = NULL, ..., subset, na.action = NULL, drop = FALSE, sep = ".", lex.order = FALSE)

(3) Default usage

boxplot(x, ..., range = 1.5, width = NULL, varwidth = FALSE, notch = FALSE, outline = TRUE, names, plot = TRUE, border = par("fg"), col = NULL, log = "", pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5), horizontal = FALSE, add = FALSE, at = NULL)

The meaning of the main parameters:

x : vector, list or data frame.

formula: formula, in the form of y~grp, where y is a vector, and grp is a grouping of data, usually a factor.

data: A data frame or list to provide the data in the formula.

range: value, the default is 1.5, indicating the range of the tentacles, that is, range × (Q3 - Q1)

width: The relative width of the box, which is valid when there are multiple boxes.

varwidth: Logical value, controlling the width of the box, it only works when there are multiple boxes in the picture, the default is FALSE, all the boxes have the same width, when its value is TRUE, it represents the sample size of each box as its relative width

notch: Logical value, if this parameter is set to TRUE, notches will appear on both sides of the box. The default is FALSE.

outline: Logical value, if this parameter is set to FALSE, no outliers will be drawn in the boxplot. The default is TRUE.

names: The grouping labels drawn below each boxplot.

plot : Logical value, whether to draw a boxplot, if set to FALSE, the boxplot will not be drawn, but the relevant information for drawing the boxplot, such as the information of 5 points, etc. will be given.

border: The border color of the boxplot.

col: the fill color of the boxplot.

horizontal: logical value, specifies whether the boxplot is drawn horizontally, the default is FALSE.

Example of boxplot function usage

(1) Simple usage

The statistics of the height data of a middle school student are as follows:

144, 166, 163, 143, 152, 169, 130, 159, 160, 175, 161, 170, 146, 159, 150, 183, 165, 146, 169

Draw its boxplot.

h <- c(144,166,163,143,152,169,130,159,160,175,161,170, 146,159,150,183,165,146,169)

boxplot(h)

The result is shown in the figure below:

simple boxplot

(2) Boxplots of multiple groups

A factory implements a new working method, the working efficiency (output per hour) of the experimental group and the control group (original method), as shown in the following data:

Test groups: 35, 41, 40, 37, 43, 32, 39, 46

Control group: 32, 39, 34, 36, 32, 38, 34, 31

Draw its boxplot.

Write the R program as follows:

x <- c(35, 41, 40, 37, 43, 32, 39, 46, 32, 39, 34, 36, 32, 38, 34, 31)

f <- factor(rep(c("test group","control group"), each=8)) #define the grouping factor

data<- data.frame(x,f) #generate data frame

boxplot(x~f,data)

The result is shown in the figure below:

Multiple group comparison boxplot

(3) Use of width parameter, border parameter and col parameter

x <- c(35, 41, 40, 37, 43, 32, 39, 46, 32, 39, 34, 36, 32, 38, 34, 31)

f <- factor(rep(c("test group","control group"), each=8))

data<- data.frame(x,f)

boxplot(x~f,data,width=c(1,2), col=c(2,3), border=c("darkgray","purple"))

Results as shown below:

Adding colors to boxplots and more

As can be seen from the figure, the width of the second boxplot is twice that of the first one. The color of each wireframe and the background color are also different. This is mainly given by the width parameter, col parameter and border parameter.

(4) Box plot with notches

x <- c(35, 41, 40, 37, 43, 32, 39, 46, 32, 39, 34, 36, 32, 38, 34, 31)

f <- factor(rep(c("test group","control group"), each=8))

data<- data.frame(x, f)

boxplot(x~f,data,width=c(1,2), col=c(2,3), notch=TRUE)

As shown below:

Notched Boxplot

For the use of other parameters, readers can test and check by themselves according to relevant explanations. Here are only the usage methods of some commonly used parameters. Hope it helps you.

If you like the editor's article, please pay attention to this account. The editor will regularly update articles about the R language recently. If you are interested in R language or data analysis, you can discuss it together. In the future, this number will launch a series of articles on Python, SPSS, Excel, etc.

Guess you like

Origin blog.csdn.net/weixin_45709013/article/details/122531014