R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Stem-and-leaf plots are a method for showing the frequency distribution of a set of data points. They effectively display the distribution while retaining the original data.
In this tutorial, we'll cover:
In a stem-and-leaf plot, data are split into "stems" and "leaves." The stem is usually composed of the digits of the largest place value and the leaf is composed of the digits of the smaller place value(s). For example, for the number 47, 4 would be the stem and 7 would be the leaf.
R provides a built-in function named stem
in the base package to produce stem-and-leaf plots.
Example:
# Sample data data <- c(12, 21, 32, 33, 35, 36, 42, 44, 45, 47, 48, 49, 50, 55, 57) # Create stem-and-leaf plot stem(data)
Upon executing the above code, R will display the stem-and-leaf plot for the data. It might look something like this:
The decimal point is 1 digit(s) to the right of the | 1 | 2 2 | 13 3 | 2356 4 | 245789 5 | 057
In the output:
Using the example plot:
The stem-and-leaf plot provides a visual representation of the distribution of the dataset. The spread and shape of the data can be easily seen. For instance, you can observe clusters of data points, gaps, or outliers.
Stem-and-leaf plots offer a quick way to visualize the distribution of a dataset without losing the actual data points. They are particularly useful for smaller datasets. For larger datasets, other forms of visualization like histograms or box plots may be more appropriate. In R, creating stem-and-leaf plots is straightforward using the stem
function.
Creating stem-and-leaf displays with R:
# Example of creating a basic stem-and-leaf plot data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data)
R code for generating stem-and-leaf plots:
stem()
function in R is used to generate stem-and-leaf plots.# Example of generating a stem-and-leaf plot data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data)
Customizing stem-and-leaf plots in R:
# Example of customizing a stem-and-leaf plot data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data, scale = 2, width = 2)
Comparing stem-and-leaf plots vs. other visualizations in R:
# Example of comparing stem-and-leaf plots with a histogram data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data) hist(data)
Handling large datasets with stem-and-leaf plots in R:
# Example of handling a large dataset with a stem-and-leaf plot large_data <- rnorm(1000) stem(large_data)
Interpreting and analyzing stem-and-leaf plots in R:
# Example of interpreting a stem-and-leaf plot data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data)
Advanced features of stem-and-leaf plots in R programming:
# Example of advanced features in a stem-and-leaf plot data <- c(23, 27, 34, 35, 36, 39, 42, 43, 47, 50) stem(data, split = 2)
Using stem-and-leaf displays for exploratory data analysis in R:
# Example of using stem-and-leaf plots in exploratory data analysis data <- rnorm(100) stem(data)
Dynamic stem-and-leaf plots in Shiny apps with R:
# Example Shiny app with a dynamic stem-and-leaf plot library(shiny) ui <- fluidPage( plotOutput("stem_leaf_plot") ) server <- function(input, output) { output$stem_leaf_plot <- renderPlot({ # Dynamic stem-and-leaf plot based on user input # ... }) } shinyApp(ui, server)