Based Plot: Histogram
π Summary of R Commands Used (Histograms & Density Plots)
1. hist()
: Creates a histogram (frequency distribution).
2. lines()
Adds lines to an existing plot.
Often used with
density()
to add a smoothed curve.
Key arguments:
col
β line colorlwd
β line widthlty
β line type (solid, dashed, dotted, etc.)
3. density()
Estimates the probability density function of a numeric variable.
Returns an object that can be plotted with
plot()
orlines()
.
4. plot()
: General plotting function. Used here for density plots.
5. legend()
: Adds a legend to a plot.
6. box()
Adds a box around the plot area (optional).
col
can set the box border color.
7. rug()
- Adds tick marks at actual data values along the x-axis.
- Useful in combination with density plots.
8. rgb()
Defines colors with transparency.
rgb(red, green, blue, alpha)
where values range 0β1.Example:
rgb(1, 0, 0, 0.5)
= semi-transparent red.
10. rnorm()
: Generates random numbers from a normal distribution.
11. Advanced hist()
with plot = FALSE
hist(..., plot = FALSE)
β stores histogram object without drawing.Access
h$mids
(bin midpoints) andh$counts
(bin frequencies) for custom coloring.