Skip to main content

9 Data Visualization with ggplot2: Mastering the Grammar of Graphics

 

ggplot2, data visualization, R, scatter plot, bar chart, histogram, faceting, themes, plotly, ggiraph

ggplot2 brings the Grammar of Graphics to life in R, providing a layered approach to building publication-quality charts. Whether you need a simple scatter plot or a complex faceted dashboard, ggplot2 offers intuitive syntax for mapping data variables to visual aesthetics, controlling scales, and fine-tuning themes. In this post, you’ll learn:

  • How to build scatter plots, bar charts, and histograms

  • Techniques for mapping aesthetics like color, size, and shape

  • Customizing labels, colors, and themes for polished visuals

  • Faceting for multi-panel displays

  • Adding interactivity with plotly and ggiraph

  • Best practices for saving and optimizing plots

By the end, you’ll be equipped to transform raw data into compelling visual narratives that inform, engage, and persuade.

1. Getting Started with ggplot2

Install and load ggplot2 (part of the tidyverse) before you begin:

r
install.packages("ggplot2")
library(ggplot2)

Import or convert your data into a tibble for seamless integration:

r
library(tibble)
df <- as_tibble(iris)  # example dataset

A basic ggplot2 workflow follows three steps:

  1. Initialize a ggplot object with your data and global mappings (aes()).

  2. Add layers (geom_) to represent data points, bars, lines, etc.

  3. Customize with scales, labels, and themes.

r
ggplot(df, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point()

This creates a scatter plot of sepal versus petal length, with default settings.

2. Building Scatter Plots

Scatter plots are ideal for spotting relationships and outliers in two continuous variables.

2.1 Basic Scatter Plot

r
ggplot(df, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point()

2.2 Mapping Aesthetics

Map additional variables to aesthetics:

  • color for categories

  • size for magnitude

  • shape for groups

r
ggplot(df, aes(x = Sepal.Length, y = Petal.Length,
               color = Species, size = Petal.Width)) +
  geom_point(alpha = 0.7)
  • alpha controls transparency to reduce overplotting.

  • Use scale_color_manual() or scale_color_brewer() to customize palettes.

2.3 Adding Smooth Lines

Add a trend line with geom_smooth():

r
ggplot(df, aes(Sepal.Length, Petal.Length)) +
  geom_point() +
  geom_smooth(method = "lm", color = "darkblue", se = FALSE)
  • method = "lm" fits a linear model.

  • se = TRUE (default) displays confidence intervals.

3. Creating Bar Charts

Bar charts summarize counts or aggregate values for categorical variables.

3.1 Count Bars

r
ggplot(df, aes(x = Species)) +
  geom_bar(fill = "steelblue")
  • Default stat = "count"; y-axis shows row counts.

3.2 Summarized Bars

Map a summary statistic, such as mean or sum, with stat_summary() or by pre-aggregating:

r
# Pre-aggregate with dplyr
library(dplyr)

means <- df %>%
  group_by(Species) %>%
  summarize(avg_sepal = mean(Sepal.Length))

ggplot(means, aes(x = Species, y = avg_sepal)) +
  geom_col(fill = "tomato")

Or use geom_col() (which uses the y aesthetic):

r
ggplot(df, aes(x = Species, y = Sepal.Length)) +
  stat_summary(fun = "mean", geom = "col", fill = "tomato")

3.3 Adding Error Bars

r
ggplot(df, aes(Species, Sepal.Length)) +
  stat_summary(fun = "mean", geom = "col", fill = "lightgreen") +
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2)
  • mean_se computes mean ± standard error.

4. Drawing Histograms and Density Plots

Histograms and density plots reveal distributions of a single continuous variable.

4.1 Histograms

r
ggplot(df, aes(x = Sepal.Length)) +
  geom_histogram(binwidth = 0.3, fill = "cornflowerblue", color = "white")
  • Adjust binwidth or use bins = 30 for control over bar count.

4.2 Density Curves

r
ggplot(df, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.4)
  • Overlay densities by group.

  • Use position = "identity" versus position = "stack" to control stacking.

5. Customizing Colors, Labels, and Themes

Polished visuals require thoughtful styling. Use scales, labs, and themes.

5.1 Scales for Colors and Sizes

r
+ scale_color_brewer(palette = "Dark2")
+ scale_fill_viridis_d(option = "C")         # from viridis package
+ scale_size_continuous(range = c(2, 8))

5.2 Labels and Titles

r
+ labs(
    title    = "Sepal vs Petal Dimensions by Species",
    subtitle = "Iris dataset visualization with ggplot2",
    x        = "Sepal Length (cm)",
    y        = "Petal Length (cm)",
    color    = "Species",
    size     = "Petal Width"
  )

5.3 Themes

ggplot2 offers built-in themes:

  • theme_minimal()

  • theme_bw()

  • theme_classic()

  • theme_dark()

r
+ theme_minimal(base_size = 14) +
  theme(
    plot.title   = element_text(face = "bold", hjust = 0.5),
    axis.text    = element_text(color = "gray30"),
    legend.position = "bottom"
  )

Use theme() to override specific elements without rebuilding an entire theme.

6. Faceting for Multi-Panel Displays

Faceting creates grids of plots split by one or two categorical variables, revealing patterns at a glance.

6.1 facet_wrap()

r
ggplot(df, aes(Sepal.Length, Petal.Length, color = Species)) +
  geom_point() +
  facet_wrap(~ Species, ncol = 3)

6.2 facet_grid()

r
# by species and another category (if available)
ggplot(df, aes(Sepal.Length, Petal.Length)) +
  geom_point() +
  facet_grid(~ Species)
  • Use rows ~ cols syntax in facet_grid().

  • Control scales independently: scales = "free_x" or "free".

7. Interactive Extensions: plotly and ggiraph

Turn static ggplot2 charts into interactive visualizations for web reports and dashboards.

7.1 Converting to plotly

r
install.packages("plotly")
library(plotly)

p <- ggplot(df, aes(Sepal.Length, Petal.Length, color = Species)) +
     geom_point()
ggplotly(p)
  • ggplotly() preserves layers, tooltips, and legend interactivity.

7.2 Using ggiraph

r
install.packages("ggiraph")
library(ggiraph)

p_ggiraph <- ggplot(df, aes(Sepal.Length, Petal.Length,
                            tooltip = Species, data_id = row_number())) +
             geom_point_interactive(size = 3, alpha = 0.8)

girafe(ggobj = p_ggiraph)
  • geom_*_interactive() adds hover tooltips and click actions.

  • Embed resulting girafe() widget in R Markdown or Shiny apps.

8. Saving and Exporting Plots

Use ggsave() to export your ggplot2 charts to files:

r
ggsave(
  filename = "plots/scatter_sepal_petal.png",
  plot     = last_plot(),
  width    = 8, height = 5, dpi = 300
)
  • Supports pdf, svg, eps, jpeg, and more by file extension.

  • Control dimensions and resolution for print or web.

9. Best Practices for Effective Visualization

  • Start with a clear question: design charts that answer specific analytical or communication needs.

  • Avoid clutter: remove unnecessary gridlines, legends, or background elements.

  • Use color wisely: leverage colorblind-friendly palettes and limit categorical levels.

  • Leverage faceting over color when too many groups complicate a single plot.

  • Annotate thoughtfully: call out key points with geom_text() or annotate().

  • Iterate and solicit feedback: share drafts with peers to refine clarity and impact.

10. Conclusion and Next Steps

With ggplot2’s Grammar of Graphics, you now have a systematic framework to build virtually any chart in R. From scatter plots and histograms to customized, interactive dashboards, mastering layers, aesthetic mappings, faceting, and themes empowers you to tell compelling data stories.

In the next post, we’ll cover String and Date–Time Handling using stringr and lubridate to prepare messy real-world data for analysis and visualization. If you have favorite ggplot2 tricks, palette recommendations, or interactive chart examples, please share them in the comments below. Happy plotting!

Comments

Popular posts from this blog

Alfred Marshall – The Father of Modern Microeconomics

  Welcome back to the blog! Today we explore the life and legacy of Alfred Marshall (1842–1924) , the British economist who laid the foundations of modern microeconomics . His landmark book, Principles of Economics (1890), introduced core concepts like supply and demand , elasticity , and market equilibrium — ideas that continue to shape how we understand economics today. Who Was Alfred Marshall? Alfred Marshall was a professor at the University of Cambridge and a key figure in the development of neoclassical economics . He believed economics should be rigorous, mathematical, and practical , focusing on real-world issues like prices, wages, and consumer behavior. Marshall also emphasized that economics is ultimately about improving human well-being. Key Contributions 1. Supply and Demand Analysis Marshall was the first to clearly present supply and demand as intersecting curves on a graph. He showed how prices are determined by both what consumers are willing to pay (dem...

Fundamental Analysis Case Study NVIDIA

  Executive summary NVIDIA is analyzed here using the full fundamental framework: balance sheet, income statement, cash flow statement, valuation multiples, sector comparison, sensitivity scenarios, and investment checklist. The company shows exceptional profitability, strong cash generation, conservative liquidity and net cash, and premium valuation multiples justified only if high growth and margin profiles persist. Key investment considerations are growth sustainability in data center and AI, margin durability, geopolitical and supply risks, and valuation sensitivity to execution. The detailed numerical work below uses the exact metrics you provided. Company profile and market context Business model and market position Company NVIDIA Corporation, leader in GPUs, AI accelerators, and related software platforms. Core revenue streams : data center GPUs and systems, gaming GPUs, professional visualization, automotive, software and services. Strategic advantage : GPU architecture, C...

“This Sentence Is False”: The Liar Paradox, from Ancient Crete to Modern Code

 “All Cretans are liars,” said the Cretan Epimenides.  “This sentence is false,” echoes every logic textbook.  We’re still arguing 2,600 years later—and the paradox is winning.   _____________________________  /                             \ |   “THIS SENTENCE IS FALSE.”  |  \_____________________________/               |               |  self-reference               v    +---------------------------+    |  Truth flips back on     |    |  itself — paradox loop!  |    +---------------------------+ 1. Meet the Liar The classic one-liner: L: “This sentence is false.” If L is true, then what it asserts—its own falsity—must hold, so L is false. If L is false, then what it asserts isn’t the ca...