Showing posts with label Chapter-3:- Histogram in R-programming. Show all posts
Showing posts with label Chapter-3:- Histogram in R-programming. Show all posts

Wednesday, January 21, 2026

Chapter-3:- Histogram in R-programming

Example-1:- Generate random number by mean and standard deviation 

Code:- 
# Generate sample data
data <- rnorm(100, mean = 50, sd = 10)

# Create histogram with custom parameters
hist(data, 
     breaks = 10,               # Number of bins
     freq = FALSE,              # Density scale (needed for overlay)
     col = "lightblue",         # Fill color
     border = "darkblue",       # Border color
     main = "Histogram Example",# Title
     xlab = "Measurements",      # X-axis label
     xlim = c(20, 80),          # X-axis limits
     density=50, angle=30, label=TRUE)

# Overlay a density curve
lines(density(data), col = "red", lwd = 2, lty="dashed")

Solution:-
Explanation:
  • In code we have to use "rnorm" if we have to generate random number, also use "mean" & "sd" because these are parameter 
  • breaks= denote the number of bins if we put here 10 it means 10 bins, but always not 10 it can vary according to data may be +/- 5 

  • freq=FALSE; if we use then it use density & freq=TRUE use frequency:-

  • Use freq = FALSE (Density) when: 
  • a) Comparing different sample sizes:- If you want to compare a survey of 100 people against a survey of 10,000 people, you cannot use counts because the larger survey's bars will tower over the smaller one. Density scales them both so they are comparable. 
  • b) Overlaying a Density Curve: If you want to draw a smooth "bell curve" line over your histogram using lines(density(data)), you must use freq = FALSE. Otherwise, the line will be so small it looks like it's lying on the floor (at 0.0). 
  • c) Probability analysis: You want the total area of the histogram to equal 1 (100%), which represents the probability distribution of the data. 

  • Use freq = TRUE (Frequency) when: 
  • a) You need raw counts: You want to know exactly how many items fall into each bin (e.g., "Exactly 15 people are in the 20-30 age group"). 
  • b) Simple reporting: You are presenting data to a general audience who finds "counts" easier to understand than "densities." 
  • c) Single dataset: You are only looking at one group of data and don't need to compare it to a mathematical model. 
  • d) Note: This is the default setting in R. 

  • density=50 means bar color 50% area cover
  • angle=30 means line draw in bar is at 30 degree angle
  • lines is use to draw line but it use only when freq=FALSE otherwise in case of freq=TRUE it show flat line
  • lwd=2 means thickness of line
  • lty= show dashed line

All parameter of hist() :-

Data and Binning:-

x

The numeric data vector for the histogram

breaks

Defines the bins, accepting vectors, numbers, algorithms like “Sturges”, or function

nclass

An alternative to breaks for compatibility


Frequency vs Density:-

freq

If TRUE (default), plot counts; If FALSE, plot probability densities

probability

An alias for !freq


Bin Intervals:-

include.lowest

If TRUE, includes values equal to breakpoints in the first or last bin

right

If TRUE (default), intervals are right-closed (a,b)


Appearance:-

col

Color for the bars

border

Color for bar borders

density

Shading line density

angle

Angle of shading lines

labels

Adds labels on bars if not FALSE


Labels and Axes:-

main

Main title

xlab

X-axis label

ylab

Y-axis label (default to “frequency” if freq is true

xlim/ ylim

Axis limits

axes

If TRUE (default), axes are drawn


Behavior:-

plot

If TRUE (default), draws the histogram; if FALSE return only calculation

warn.unused

Warns about unused graphical parameters if plot=FALSE





Example-2:- How to write Histogram if we have given data:-

Code:-
data <- c(13,23,23,45,66,85,41,38,98,84,68) 

# Create histogram with custom parameters
hist(data, 
     breaks = 30,
     freq = FALSE,
     col = "lightblue",           # Fill color
     border = "yellow",           # Border color
     main = "Histogram Example",   # Title
     xlab = "Measurements",      # X-axis label
     xlim = c(10, 100),           # X-axis limits example here 20 to 80
     density=50,angle=30, label=TRUE)         

lines(density(data), col = "red", lwd = 2,lty="dashed")

Solution:-


Explanation:- Same as above example, only difference here we have given data, in previous we generate data



Chapter-5:- Scatter Plot in R-programming

 Chapter-5:- Scatter Plot in R-programming Example-1:-  Create a scatter plot of car weight on x-axis and miles per gallon on y-axis by pre-...