A histogram is a type of bar chart which shows the frequency of the number of values which are compared with a set of values ranges. The histogram is used for the distribution, whereas a bar chart is used for comparing different entities. In the histogram, each bar represents the height of the number of values present in the given range.
In R, a histogram is a graphical representation of the distribution of a dataset. It provides a visual summary of the underlying probability distribution of a set of continuous or discrete data. The data is divided into intervals called bins, and the number of data points falling into each bin is represented by the height of a bar.
To create a histogram in R, you can use the hist()
function. Here’s a simple explanation of how to use it:
# Create a vector of data
data <- c(22, 25, 30, 33, 35, 38, 40, 42, 45, 50)
# Create a histogram
hist(data,
main = "Histogram of Data", # Title of the plot
xlab = "Values", # Label for the x-axis
ylab = "Frequency", # Label for the y-axis
col = "skyblue", # Color of the bars
border = "black", # Color of the border of bars
breaks = 5 # Number of bins or breaks
)
In this example:
data
is the vector of your dataset.main
,xlab
, andylab
are optional parameters to provide a title and labels for the x and y axes.col
andborder
specify the color of the bars and their borders, respectively.breaks
determines the number of bins.
The resulting plot will display a histogram with bars representing the frequency of values within each bin.
Remember to adjust parameters such as breaks
based on the characteristics of your data and the level of granularity you want in the histogram. This is just a basic example, and R provides many options for customizing histograms based on your specific needs.