Automated summary
A practical guide for building, reviewing, and refining ggplot2 visualizations in R, drawn from Kieran Healy's book on data visualization.
Strengths
- Spans the full ggplot2 workflow, from tidy data through scales, labels, and final themes
- Grounds chart-type and color-palette decisions in visual perception principles
- Includes dedicated coverage of model-result plots, maps with sf/statebins, and figure polishing with patchwork
- Requires reading targeted reference files before answering, which keeps advice structured and specific
Limitations
- Scoped to the R/ggplot2 ecosystem, with no Python or JavaScript equivalents
- The CC-BY-NC-ND license restricts reuse and redistribution of the source material
- The provided description is truncated, so the full list of reference files is not visible
Use it whenever someone wants to create, fix, or improve a ggplot2 chart or pick a suitable visual encoding for their data.
Avoid it for visualizations in other libraries or languages (matplotlib, D3, Plotly.js) or for interactive dashboards, which fall outside this skill's scope.
Security analysis
SafeThe skill only uses Read, Edit, Write, Grep, Glob, and R documentation MCP tools. It instructs the agent to create or improve ggplot2 visualizations and consult reference files; there are no destructive commands, network calls, obfuscated payloads, or data exfiltration patterns. The R MCP tools are limited to documentation lookup, not arbitrary code execution beyond typical plotting workflows.
No concerns found
How this analysis is madeExamples
Here's my ggplot2 scatterplot of income vs. life expectancy with points colored by region and sized by population — it looks messy. Can you help me clean it up and make it publication-ready?I have categorical survey responses broken down by age group and gender. Which chart type should I use in ggplot2, and how do I avoid misleading my readers?I fit a logistic regression in R and want to plot the predicted probabilities with confidence intervals using ggplot2. How should I structure this?name: socviz description: | Data visualization with ggplot2 based on Kieran Healy's "Data Visualization: A Practical Introduction" (2nd ed., 2026). Use when creating, reviewing, or improving ggplot2 visualizations, choosing appropriate chart types, applying perceptual principles, faceting and grouping data, labeling and annotating plots, visualizing model results, drawing maps, or polishing figures for publication. Covers the grammar of graphics workflow, visual perception, color palettes, layered plot construction, and theme customization. license: CC-BY-NC-ND metadata: source: https://socviz.co author: Kieran Healy edition: 2nd (2026) publisher: Princeton University Press allowed-tools: Read, Edit, Write, Grep, Glob, mcp__r-btw__*
Data Visualization with ggplot2
Based on: Data Visualization: A Practical Introduction (2nd ed.) Author: Kieran Healy (2026). Princeton University Press. Free online: https://socviz.co
Core philosophy
Visualization is a tool for understanding data, not decoration. Every graph encodes data through visual channels (position, length, color, shape) that the human perceptual system decodes with varying accuracy. Good visualization requires understanding both the grammar of graphics and the perceptual system that interprets the output.
The ggplot2 workflow
Every visualization follows the same five steps:
- Tidy data -- Long format: every variable a column, every observation a row
- Mapping --
aes()connects data variables to visual properties (x, y, color, fill, size, shape) - Geom -- Geometric objects render the mapping (points, lines, bars, etc.)
- Scales and coordinates -- Transform axes, set breaks, choose projections
- Labels and guides -- Titles, legends, annotations, themes
p <- ggplot(data = df,
mapping = aes(x = var1, y = var2, color = group))
p + geom_point() +
scale_x_log10(labels = scales::label_comma()) +
labs(x = "X Label", y = "Y Label", title = "Title")
When to use this skill
- User wants to create or improve a ggplot2 visualization
- User asks which chart type to use for their data
- User needs help with faceting, grouping, labeling, or annotating plots
- User wants to visualize model results (coefficients, predictions, residuals)
- User asks about color palettes, themes, or publication-quality figures
- User wants to draw maps with sf or statebins
- User asks about perceptual principles for data visualization
Instructions
-
Classify the request:
- Perceptual/design: Which chart type? What color palette? How to avoid misleading? --> Use
references/perception-principles.md - Building a plot: How to facet, group, layer, label? --> Use the relevant reference file
- Model visualization: Coefficients, predictions, residuals? --> Use
references/model-visualization.md - Maps: Choropleth, cartogram, spatial data? --> Use
references/maps-spatial.md - Polishing: Themes, color, layout, patchwork? --> Use
references/themes-polish.md
- Perceptual/design: Which chart type? What color palette? How to avoid misleading? --> Use
-
Read the relevant reference files before answering
-
Apply these principles in every response:
- Match visual encoding to data type (position for quantitative, color hue for categorical)
- Prefer position on common scale over length, angle, or area
- Use tidy (long-format) data; reshape before plotting if needed
- Build plots layer by layer; inspect at each step
- Distinguish mapping (inside
aes()) from setting (insidegeom_*()) - Calculate summaries with dplyr before graphing when
geom_bar()/stat_count()gets confusing - Use
facet_wrap()orfacet_grid()instead of cramming too much into one panel
-
Follow write-r conventions for all R code (native pipe
|>,.by,join_by(), etc.)
Available resources
Reference files (references/)
| File | Topic | Covers |
|------|-------|--------|
| perception-principles.md | Visual perception and chart choice | Preattentive features, gestalt rules, Cleveland-McGill ranking, color models, honesty/judgment tradeoffs |
| ggplot-fundamentals.md | Core ggplot2 patterns | Tidy data, aes() mapping vs setting, layering geoms, scales, saving plots |
| grouping-faceting.md | Grouped data and faceting | group aesthetic, facet_wrap/facet_grid, stat transformations, histograms, density plots |
| labels-annotations.md | Text, labels, and annotations | Pipes for summarization, text/label geoms, ggrepel, annotate(), scales/guides/themes framework |
| model-visualization.md | Visualizing model results | broom (tidy/augment/glance), coefficient plots, residuals, nested models, survey data, marginal effects |
| maps-spatial.md | Maps and spatial data | sf objects, geom_sf, choropleths, cartograms, statebins, viridis palettes, MAUP |
| themes-polish.md | Polishing and customization | Color palettes, layer-highlight-repeat, theme(), patchwork layouts, workhorses vs show ponies |
Function reference (via MCP, not bundled)
- R: Use
btw_tool_docs_help_page(package_name="ggplot2", topic="<function>")for live docs - R: Use Context7 with library ID for ggplot2 code examples
- Key packages:
ggplot2,scales,ggrepel,patchwork,sf,statebins,broom,colorspace
Quick reference: choosing a geom
| Data situation | Recommended geom | Avoid |
|----------------|------------------|-------|
| Two continuous variables | geom_point() | |
| Continuous + trend | geom_point() + geom_smooth() | |
| One continuous, one categorical | geom_boxplot(), geom_violin(), geom_jitter() | |
| Single continuous distribution | geom_histogram(), geom_density() | |
| Counts of categories | geom_bar() | Pie charts |
| Pre-computed summary | geom_col() or geom_bar(stat = "identity") | |
| Coefficients with CIs | geom_pointrange() | Bar charts for coefficients |
| Time series by group | geom_line(aes(group = ...)) | |
| Trajectories + trend | Gray geom_line() + colored geom_smooth() | |
| Spatial/geographic | geom_sf() | geom_polygon() with map_data (legacy) |
| Two categorical cross-tab | facet_grid(row ~ col) | Stacked bars (except bottom category) |
| Many categories | Horizontal layout (categories on y-axis) | Rotated x-axis labels |
| Labeled points | ggrepel::geom_text_repel() | geom_text() without repulsion |
Key anti-patterns
| Avoid | Do instead | Why |
|-------|-----------|-----|
| Pie charts | Faceted bar charts or dot plots | Angles decoded poorly (Cleveland-McGill) |
| 3D effects | Flat 2D encodings | Perspective distorts values |
| Stacked bars (comparing middle categories) | Faceted bars or Cleveland dot plots | Only bottom category has stable baseline |
| Mapping a string inside aes() | Set property in geom_*() directly | aes(color = "purple") creates unwanted legend |
| geom_bar() for pre-computed data | geom_col() | geom_bar() counts rows by default |
| Default alphabetical ordering | reorder(category, value) | Meaningful order aids comparison |
| Too many channels at once | Facet or simplify | Shape + color + size overwhelms preattentive processing |
| Picking colors ad hoc | Use ColorBrewer, viridis, or colorspace palettes | Perceptually uniform, colorblind-safe |
| + geom_point() on next line | geom_point() + at end of line | ggplot + must end the line, not begin one |
Example: complete workflow
library(tidyverse)
library(scales)
library(ggrepel)
# 1. Summarize data
by_continent <- gapminder |>
summarize(
mean_life = mean(lifeExp),
mean_gdp = mean(gdpPercap),
n_countries = n_distinct(country),
.by = continent
)
# 2. Build plot layer by layer
p <- ggplot(by_continent,
aes(x = mean_gdp, y = mean_life))
p +
geom_point(aes(size = n_countries), alpha = 0.7) +
geom_text_repel(aes(label = continent)) +
scale_x_log10(labels = label_currency()) +
scale_size_continuous(range = c(3, 10)) +
labs(
x = "Mean GDP per Capita",
y = "Mean Life Expectancy (years)",
size = "Countries",
title = "Economic Development and Life Expectancy",
caption = "Source: Gapminder."
) +
theme_minimal() +
theme(legend.position = "bottom")
Best practices
- Always build plots incrementally and inspect at each layer
- Use
ggsave()with explicit dimensions and vector formats (PDF, SVG) for publication - Organize figures in a
figures/subdirectory; usehere::here()for paths - Use
guides(fill = "none")to remove redundant legends - When debugging bizarre output, check the relationship between data structure and aesthetic mappings first
- Prefer faceting over dodging for grouped comparisons
- Place categories on the y-axis when there are many levels
- Order categories by the value being displayed, not alphabetically
- Use
scales::label_*()functions for axis formatting
From the file to a first result
The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.
View the source and supporting filesThis download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.
1. Download the file
Download SKILL.mdFrom the root of a test project, save the file as .skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.
Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.
mkdir -p '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/data-visualization-with-ggplot2-mrwakc5u/download' --output '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md.part' && mv '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md.part' '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md'2. Check prerequisites
Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.
Tools declared by the author (check availability in your agent): Read, Edit, Write, Grep, Glob, mcp__r-btw__*
Original excerpt: Quick reference: choosing a geom
| Data situation | Recommended geom | Avoid |
|---|---|---|
| Two continuous variables | geom_point() | |
| Continuous + trend | geom_point() + geom_smooth() | |
| One continuous, one categorical | geom_boxplot(), geom_violin(), geom_jitter() | |
| Single continuous distribution | geom_histogram(), geom_density() | |
| Counts of categories | geom_bar() | Pie charts |
| Pre-computed summary | geom_col() or geom_bar(stat = "identity") | |
| Coefficients with CIs | geom_pointrange() | Bar charts for coefficients |
| Time series by group | geom_line(aes(group = ...)) | |
| Trajectories + trend | Gray geom_line() + colored geom_smooth() | |
| Spatial/geographic | geom_sf() | geom_polygon() with map_data (legacy) |
| Two categorical cross-tab | facet_grid(row ~ col) | Stacked bars (except bottom category) |
| Many categories | Horizontal layout (categories on y-axis) | Rotated x-axis labels |
| Labeled points | ggrepel::geom_text_repel() | geom_text() without repulsion |
The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.
3. Load the skill
After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.
Claude Code
test -f '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && test -s '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/socviz' && cp -R '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u' '.claude/skills/socviz'Cursor
test -f '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && test -s '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/socviz' && cp -R '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u' '.cursor/skills/socviz'Codex
test -f '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && test -s '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/socviz' && cp -R '.skill-downloads/data-visualization-with-ggplot2-mrwakc5u' '.agents/skills/socviz'OpenSkills
OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.
Official OpenSkills installationopenskills install './.skill-downloads/data-visualization-with-ggplot2-mrwakc5u'openskills read 'data-visualization-with-ggplot2-mrwakc5u'Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.
For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.
openskills syncManual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.
4. Try a first task
Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:
Read .skill-downloads/data-visualization-with-ggplot2-mrwakc5u/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.
Create Interactive Maps with kepler.gl
Data & AI
Create interactive map visualizations and export standalone HTML using the keplergl Python package. Use to plot geospatial data from DataFrames, GeoJSON, or CSV.
Agent-Driven Parameter Optimization
Data & AI
Guide for optimizing configurable systems with a measurable objective, using bounded trials and evidence-based recommendations.
ICP Visitor Outreach
Data & AI
Identify signed-up users matching the ideal customer profile but stalled, and draft personalized outreach to re-engage them.