SyntaxStudy
Sign Up
R Bar Charts, Histograms, Boxplots, and Facets
R Beginner 2 min read

Bar Charts, Histograms, Boxplots, and Facets

Beyond scatter plots, ggplot2 provides a rich library of geom functions for different chart types. geom_bar() and geom_col() create bar charts; geom_bar() counts rows by default (stat = "count") while geom_col() uses a y column for heights (stat = "identity"). Setting position = "dodge" places bars side by side for grouped comparisons, and position = "fill" normalises bar heights to show proportions. geom_histogram() creates histograms for continuous variables; the binwidth argument controls the width of each bin, which significantly affects the appearance and interpretation of the chart. Boxplots (geom_boxplot()) and violin plots (geom_violin()) summarise the distribution of a continuous variable across groups. The boxplot shows median, interquartile range, and outliers; the violin plot additionally shows the estimated density shape. Combining both (by layering geom_violin() and geom_boxplot()) gives a more complete picture. Adding geom_jitter() on top of a boxplot shows the individual data points without overplotting. Faceting divides a single plot into a panel grid based on one or two categorical variables. facet_wrap(~ variable) arranges panels in a single wrapped row (like a ribbon), while facet_grid(row_var ~ col_var) creates a strict two-dimensional grid. Both functions accept a scales argument: "fixed" (default) shares axis scales across panels for accurate comparison, while "free", "free_x", or "free_y" give each panel its own scale — useful when the variables have very different ranges.
Example
library(ggplot2)
data(mpg)
data(diamonds)

# geom_bar — counts automatically
ggplot(mpg, aes(x = class)) +
    geom_bar(fill = "steelblue") +
    labs(title = "Car count by class")

# Grouped bar chart
ggplot(mpg, aes(x = class, fill = drv)) +
    geom_bar(position = "dodge") +
    labs(title = "Class by drive type (dodged)")

# Stacked proportions
ggplot(mpg, aes(x = class, fill = drv)) +
    geom_bar(position = "fill") +
    labs(y = "Proportion")

# Histogram
ggplot(mpg, aes(x = hwy)) +
    geom_histogram(binwidth = 2, fill = "coral", colour = "white") +
    labs(title = "Distribution of highway MPG")

# Density plot
ggplot(mpg, aes(x = hwy, fill = drv)) +
    geom_density(alpha = 0.4) +
    labs(title = "Highway MPG density by drive type")

# Boxplot with jittered points
ggplot(mpg, aes(x = class, y = hwy)) +
    geom_boxplot(outlier.shape = NA) +
    geom_jitter(width = 0.2, alpha = 0.4, colour = "steelblue") +
    labs(title = "Highway MPG by car class")

# facet_wrap
ggplot(mpg, aes(x = displ, y = hwy)) +
    geom_point(alpha = 0.5) +
    facet_wrap(~ class, nrow = 2) +
    labs(title = "MPG vs displacement per class")

# facet_grid (row by cyl, col by drv)
ggplot(mpg, aes(x = displ, y = hwy)) +
    geom_point(alpha = 0.5) +
    facet_grid(cyl ~ drv) +
    labs(title = "Grid facet: cylinders x drive type")