Histograms and bar plots are two of the most common ways to visualize data. A histogram shows the distribution of a single numeric variable โ how often values fall into certain bins. A bar plot compares categories, showing counts or values side by side. Both are one-liners in R.
Histograms with hist()
hist() takes a numeric vector, splits it into bins, and plots the frequency count. The breaks argument controls how many bins you get โ more bins gives finer detail.
values <- c(12, 15, 13, 20, 19, 22, 18, 25, 30, 27,
24, 23, 21, 16, 14, 11, 28, 31, 26, 29)
hist(values, main = "Distribution of Values",
xlab = "Value Range", ylab = "Frequency",
col = "steelblue", border = "white",
breaks = 8)
Try it Yourself โ
Bar Plots with barplot()
barplot() takes a named vector or a table and draws bars. You can add names.arg for labels, horiz to flip horizontally, and las to rotate text.
sales <- c(Mon = 340, Tue = 520, Wed = 480,
Thu = 610, Fri = 730)
barplot(sales,
main = "Weekly Sales",
xlab = "Day", ylab = "Revenue ($)",
col = c("tomato", "gold", "skyblue",
"limegreen", "orchid"),
border = NA)
Try it Yourself โ
Customizing Appearance
Both hist() and barplot() accept many of the same customizations โ col, border, main, axis labels. For bar plots, density and angle add hatching, and horiz = TRUE flips the chart.
counts <- table(iris$Species)
barplot(counts,
main = "Iris Species Count",
col = c("#FF6B6B", "#4ECDC4", "#45B7D1"),
border = "white",
ylab = "Count",
las = 1)
Try it Yourself โ