R Program to Create a Histogram

1. Introduction

Histograms are graphical representations of the distribution of a dataset. They partition the spread of numeric data into intervals, known as bins, and show the number of observations that fall into each bin. In this guide, we'll be learning how to create a histogram using R.

2. Program Overview

The program will:

1. Create a numeric dataset.

2. Use the hist() function from base R to generate and display a histogram.

3. Customize the appearance of the histogram.

3. Code Program

# Create a numeric dataset
data <- c(12, 15, 13, 20, 22, 25, 27, 30, 10, 14, 16, 19, 21, 26, 29)

# Generate and display a histogram with main title and axis labels
hist(data,
     main = "Histogram of Data",
     xlab = "Value",
     ylab = "Frequency",
     col = "lightblue",
     border = "black")

Output:

[A histogram will be displayed with the title "Histogram of Data". The x-axis is labeled "Value" and the y-axis is labeled "Frequency". The bins are colored light blue with black borders.]

4. Step By Step Explanation

1. We start by creating a numeric dataset named "data". This dataset contains 15 numeric values. For the purpose of this example, we are using a hypothetical dataset.

2. The hist() function is then used to generate the histogram. Several parameters are passed to this function:

- The first parameter is our dataset, "data".- The "main" parameter is used to set the title of the histogram.- "xlab" and "ylab" set the labels for the x and y axes, respectively.- The "col" parameter sets the color of the bins in the histogram, in this case, to light blue.- The "border" parameter defines the color of the borders of the bins. We set it to black for clarity.

3. After executing the code, R will display a histogram in a separate window. The histogram showcases the distribution of the numeric data across different intervals or bins.

Note: The exact appearance of the histogram may vary based on the graphical settings of your R environment.

Comments