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:-
Frequency vs Density:-
Bin Intervals:-
Appearance:-
Labels and Axes:-
Behavior:-
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

No comments:
Post a Comment