Today, we will learn how to create a stem and leaf plot in the R programming language. So, follow this article to know how to plot it.
What is the Stem and Leaf plot in R?
A Stem and Leaf plot is a frequency with which specific values may occur. It essentially consists of a mechanism for graphically displaying quantitative data.
Unlike a histogram, a Stem and Leaf plot keeps the original data point to two significant figures. The order of the data facilitates the transition to order-based inference and no parametric statistics. Let’s examine how this plotting strategy functions.
Example: we record the weight of 10 students in one of the classes as follows
45, 65, 78, 66, 44, 38, 47, 56, 65, 69, 35
On these records, the stem and leaf plot will now be as follows:
3 | 58 4 | 457 5 | 6 6 | 5569 7 | 8
Here, the records are organized according to their most significant digit. The left side of the graph is the stem, while the right is the leaf. Sometimes merging the alternate rows with the row just after them can improve readability. If the number has infinite or missing values, they are ignored.
So, similar to a histogram but in text, a Stem and Leaf plot divides the data into the stem (often the initial or first few digits of the number) and the leaf to depict the distribution of quantitative data. This type of plot is also known as a stem and leaf diagram or stem and leaf display (the last digit).
The Stem Function: The Syntax
stem(x, scale = 1, width = 80 )
- x: The numeric vector, …
- scale: The size of the plot
- width: The width of the plot
How to create a Stem and Leaf plot in R?
Create a simple Stem and Leaf plot
First, to create a simple Stem and Leaf plot in R, we need to create data to plot it as follows.
# Create a data data <- c(21, 23, 25, 35, 38, 39, 45, 46)
Now, we will use the stem function in R to plot a Stem and Leaf plot:
# Create a data data <- c(21, 23, 25, 35, 38, 39, 45, 46) # Plot a Stem and Leaf stem(data)
The decimal point is 1 digit(s) to the right of the | 2 | 135 3 | 589 4 | 56
Here, you can look at an explanation of the results below
The decimal point is 1 digit(s) to the right of the | 2 | 135 # ←- 21 23 25 3 | 589 # ←- 35 38 39 4 | 56 # ←- 45 46
Create a Stem and Leaf plot with arguments
As a result of the stems above being clustered (the first stem is for 0 and 1, the second for 2, and so on). You may use the scale option to modify the plot’s height to resolve this problem as follows:
# Create a data data <- c(21, 23, 25, 35, 38, 39, 45, 46) # Plot a Stem and Leaf stem(data, scale = 2)
The decimal point is 1 digit(s) to the right of the | 2 | 13 2 | 5 3 | 3 | 589 4 | 4 | 56
Create a Stem and Leaf plot with columns of the data frame
Here, we will create a data frame for this example as follows:
# Create a data df <- data.frame( ID = c(1:6), Weight = c(23, 26, 32, 35, 36, 45), Height = c(145, 156, 160, 189, 172, 180) ) # View a dataframe df
ID Weight Height 1 1 23 145 2 2 26 156 3 3 32 160 4 4 35 189 5 5 36 172 6 6 45 180
Now, we can plot a Stem and Leaf of the Weight column below
# Create a data df <- data.frame( ID = c(1:6), Weight = c(23, 26, 32, 35, 36, 45), Height = c(145, 156, 160, 189, 172, 180) ) # Plot a Stem and Leaf of the Weight column stem(df$Weight)
The decimal point is 1 digit(s) to the right of the | 2 | 3 2 | 6 3 | 2 3 | 56 4 | 4 | 5
In conclusion, this article shared with us how to create the Stem and Leaf plot in R. Please comment if you have any questions; we hope you can plot a Stem and Leaf plot.
Have a great day!
Name of the university: HCMUS