R Language BoxPlot drawing function

Source: Internet
Author: User

BoxPlot is used to plot the box plots, we all know that BoxPlot is used to show the overall distribution of a set of data, in the R language, the way to support two types of input data

The first type: x, which specifies the data used to draw the box plot, is a vector

code example:

BoxPlot (1:100)

As follows:

The second, Formala and data two parameter designations, suitable for displaying the distribution of multiple sets of data

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), each =))) BoxPlot (value ~ Grou P, DataSet)

As follows:

Several commonly used parameters are explained in detail below:

1) Widh: Control the width of the box, when there are multiple boxes in the diagram will play a role, its value is the same length and number of boxes of the same vector, specifying the relative width of the different boxes

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), each =))) BoxPlot (value ~ Grou P, DataSet, Width = C (1, 2))

As follows:

As you can see, the second box is twice times the width of the first box.

2) Varwidth: Logical value, control the width of the box, only when there are multiple boxes in the diagram to play a role, the default is False, all the box width is the same, when its value is true, represents the sample size of each box as its relative width

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), times = C ()))) BoxPlot (Valu E ~ Group, dataset, Varwidth = T)

As follows:

It can be seen that the width of the two boxes is different, because the sample volume of the two sets of data is different;

3) Notch: logical value,

code example:

Par (Mfrow = C (1, 2)) BoxPlot (1:100, notch = t, main = "notch = t") BoxPlot (1:100, notch = f, main = "notch = f")

As follows:

4) Col: Fill Color of box

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), times = C ()))) BoxPlot (Valu E ~ Group, dataset, col = C ("Green", "Red"))

As follows:

5) Border: Color of the line in the box, default to Black

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), times = C ()))) BoxPlot (Valu E ~ Group, dataset,  border = C ("Red", "green"))

As follows:

6) Names: label under each box

code example:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), times = C ()))) BoxPlot (Valu E ~ Group, DataSet,  names = C ("Red", "green"))

As follows:

Below we discuss, how to decide the position of the box in the case line diagram?

By default, the center point of each box and the position of the box is certain, such as the second box corresponding to the center point of the x-coordinate is 2

What is the width of each box by default?

When there is only one box in the diagram, look at the result of the following code:

BoxPlot (1:100) abline (v = 0.8, lty = 2) abline (v = 1.2, lty = 2) axis (side = 1)

The results are as follows:

As can be seen, because there is only one box, so the center point is 1, the width of the two sides 0.2, so the left X coordinate is 0.8, the right x-axis coordinates 1.2

When there are multiple boxes in the diagram, look at the results of the following code:

DataSet <-Data.frame (value = Rep (1:100, times = 2), group = factor (Rep (c ("A", "B"), times = C (+))), BoxPlot (Val UE ~ Group, DataSet) Abline (v = 0.6, lty = 2) abline (v = 1.4, lty = 2) abline (v = 1.6, lty = 2) abline (v = 2.4, lty = 2)

The results are as follows:

We can see that the width of each box is 0.8

R Language BoxPlot drawing function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.