Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

R Language Dictionary

  1. Home
  2. R Language Dictionary
  3. ggplot2 (Basic Graphs)

ggplot2 (Basic Graphs)

R's visualization package ggplot2 is built on the Grammar of Graphics philosophy, which constructs graphs as a stack of layers. You define the canvas and data with ggplot(), map variables to visual elements (x-axis, y-axis, color, etc.) with aes(), and build the graph by layering geometries such as points, bars, and lines using geom_*() functions.

Syntax

# -----------------------------------------------
# ggplot() — define the canvas and data
# -----------------------------------------------

ggplot(data = df, mapping = aes(...))
# data    : the data frame to use for the graph
# mapping : an aes() call that maps variables to visual elements

# -----------------------------------------------
# aes() — aesthetic mapping
# -----------------------------------------------

aes(x = x_column, y = y_column, color = color_column,
    fill = fill_color, size = size, shape = shape,
    linetype = linetype, alpha = transparency, group = group)
# Column names are written without quotes (ggplot2's NSE: Non-Standard Evaluation)

# -----------------------------------------------
# geom_point() — scatter plot
# -----------------------------------------------

geom_point(mapping = NULL, data = NULL,
           size = 1.5, color = "black", alpha = 1, shape = 16)
# size  : point size (numeric)
# shape : point shape (integer 0–25)
# alpha : transparency (0.0–1.0)

# -----------------------------------------------
# geom_bar() / geom_col() — bar chart
# -----------------------------------------------

geom_bar(mapping = aes(x = column), stat = "count")  # bar height = row count
geom_col(mapping = aes(x = column, y = value_column)) # bar height = y value directly

# -----------------------------------------------
# geom_line() — line chart
# -----------------------------------------------

geom_line(mapping = aes(x = column, y = column, group = group_column),
          linewidth = 1, color = "black")
# group : specifies grouping when drawing multiple series

# -----------------------------------------------
# labs() / theme() — titles and themes
# -----------------------------------------------

labs(title = "Title", x = "X axis label", y = "Y axis label",
     color = "Legend label", caption = "Caption")
theme_minimal()   # minimal theme (others: theme_bw(), theme_classic(), etc.)

# -----------------------------------------------
# ggsave() — save to file
# -----------------------------------------------

ggsave(filename = "output.png", plot = p, width = 8, height = 5, dpi = 150)
# filename : output filename (format determined by extension)
# plot     : graph object to save (defaults to the most recent graph)
# dpi      : resolution (96–150 is typical for screen use)

Syntax Reference

Syntax / FunctionDescription
ggplot(data, aes(...))Creates the graph canvas and defines the data frame and aesthetic mapping to use.
aes(x, y, color, ...)Maps data columns to visual elements such as position, color, shape, and size. Column names are written without quotes.
geom_point()Draws scatter plot points. Appearance is adjusted with size, shape, and alpha.
geom_bar()Draws a bar chart where bar height represents the row count for the x column. Defaults to stat="count".
geom_col()Draws a bar chart where bar height equals the y column value directly. Used with pre-aggregated data.
geom_line()Draws a line chart. Use group or color mapping when drawing multiple series.
geom_histogram()Draws a histogram. Specify the number or width of bins with bins or binwidth.
geom_boxplot()Draws a box plot. Visualizes the median, interquartile range, and outliers of a distribution.
geom_text(aes(label=col))Adds text labels to each data point.
labs(title, x, y, ...)Sets the graph title, axis labels, legend labels, and caption.
theme_minimal()Applies a minimal theme that retains background gridlines but removes the panel border.
theme_bw()Applies the classic white background with black border theme.
scale_fill_manual(values=...)Manually specifies fill colors. scale_color_manual() applies to line and point colors.
facet_wrap(~ col)Splits the graph into subplots (facets) for each value of the specified column.
ggsave(filename, plot, ...)Saves the graph to a file such as PNG, PDF, or SVG. Width, height, and resolution can be specified.

Sample Code

steinsgate_ggplot2_basic.R
# steinsgate_ggplot2_basic.R — sample to explore ggplot2 basics (layer structure)
# Uses Steins;Gate character data to demonstrate
# scatter plots, bar charts, line charts, and PNG export

library(ggplot2)

# -----------------------------------------------
# Data preparation — Steins;Gate lab member stats
# -----------------------------------------------

members <- data.frame(
  name       = c("Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri", "Hashida Itaru", "Kiryu Moeka"),
  role       = c("Mad Scientist", "Genius Physicist", "Cosplayer", "Hacker", "Programmer"),
  iq         = c(128, 170, 95, 145, 138),
  d_mail     = c( 42,  18, 30,  55,  25),
  divergence = c(1.048596, 1.130205, 0.523299, 0.571024, 0.337187)
)

cat("=== Lab Member Stats ===\n")
print(members)
cat("\n")

# -----------------------------------------------
# Scatter plot — IQ vs D-mail count
# -----------------------------------------------

# Create canvas with ggplot(), map x/y with aes()
# Add geom_point() with the + operator to draw points
p_scatter <- ggplot(data = members, mapping = aes(x = iq, y = d_mail)) +
  geom_point(aes(color = name), size = 5, alpha = 0.85) +
  geom_text(aes(label = name), vjust = -1.0, size = 3.2) +
  labs(
    title   = "Steins;Gate Lab Members — IQ vs D-mail Count",
    x       = "IQ",
    y       = "D-mail Count",
    color   = "Member",
    caption = "* Values are fictional"
  ) +
  xlim(80, 185) +
  ylim(10, 65) +
  theme_minimal(base_size = 13)

ggsave("steinsgate_scatter.png", plot = p_scatter, width = 8, height = 5, dpi = 150)
cat("Scatter plot saved to steinsgate_scatter.png\n")

# -----------------------------------------------
# Bar chart — IQ by character
# -----------------------------------------------

# geom_col() uses the y value directly as bar height
p_bar <- ggplot(data = members, aes(x = reorder(name, -iq), y = iq, fill = name)) +
  geom_col(width = 0.65, color = "white", show.legend = FALSE) +
  geom_text(aes(label = iq), vjust = -0.5, size = 4) +
  labs(
    title = "Steins;Gate Lab Members — IQ Comparison",
    x     = "Member",
    y     = "IQ"
  ) +
  ylim(0, 190) +
  theme_classic(base_size = 13) +
  theme(axis.text.x = element_text(angle = 20, hjust = 1))

ggsave("steinsgate_bar.png", plot = p_bar, width = 7, height = 5, dpi = 150)
cat("Bar chart saved to steinsgate_bar.png\n")

# -----------------------------------------------
# Line chart — world line awareness score over episodes
# -----------------------------------------------

timeline <- data.frame(
  episode   = rep(1:5, times = 3),
  character = rep(c("Okabe Rintaro", "Makise Kurisu", "Hashida Itaru"), each = 5),
  awareness = c(
    10, 35, 60, 85, 100,
     5, 20, 50, 70,  95,
     2,  8, 15, 30,  55
  )
)

p_line <- ggplot(data = timeline,
                 aes(x = episode, y = awareness,
                     color = character, group = character)) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 3) +
  labs(
    title   = "Steins;Gate — World Line Awareness by Episode (fictional)",
    x       = "Episode",
    y       = "Awareness Score",
    color   = "Character",
    caption = "* Values are fictional"
  ) +
  scale_x_continuous(breaks = 1:5) +
  theme_bw(base_size = 13)

ggsave("steinsgate_line.png", plot = p_line, width = 8, height = 5, dpi = 150)
cat("Line chart saved to steinsgate_line.png\n")

# -----------------------------------------------
# Inspecting layer structure
# -----------------------------------------------

cat("\n=== ggplot Object Layer Structure ===\n")
cat("Number of layers in scatter plot:", length(p_scatter$layers), "\n")
cat("Mapped variables in scatter plot:\n")
print(names(p_scatter$mapping))
cat("\n")

cat("ggplot2 version:", as.character(packageVersion("ggplot2")), "\n")
cat("\nAll graphs saved to PNG files.\n")
Rscript steinsgate_ggplot2_basic.R
=== Lab Member Stats ===
            name             role  iq d_mail divergence
1  Okabe Rintaro    Mad Scientist 128     42   1.048596
2  Makise Kurisu Genius Physicist 170     18   1.130205
3  Shiina Mayuri        Cosplayer  95     30   0.523299
4  Hashida Itaru           Hacker 145     55   0.571024
5    Kiryu Moeka       Programmer 138     25   0.337187

Scatter plot saved to steinsgate_scatter.png
Bar chart saved to steinsgate_bar.png
Line chart saved to steinsgate_line.png

=== ggplot Object Layer Structure ===
Number of layers in scatter plot: 2
Mapped variables in scatter plot:
[1] "x" "y"

ggplot2 version: 3.5.1

All graphs saved to PNG files.

Common Mistakes

Mistake 1: Swapping x and y in aes()

Writing aes(x = y_column, y = x_column) with the axes reversed will swap the axis directions. ggplot2 draws the graph without raising an error, so the problem often goes unnoticed until you see the output. Always double-check which column is mapped to which axis.

ggplot2_aes_mistake.R
library(ggplot2)

members <- data.frame(
  name   = c("Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri", "Hashida Itaru", "Kiryu Moeka"),
  iq     = c(128, 170, 95, 145, 138),
  d_mail = c( 42,  18, 30,  55,  25)
)

# Wrong: x and y are swapped (d_mail on x-axis, iq on y-axis)
p_wrong <- ggplot(members, aes(x = d_mail, y = iq)) +
  geom_point(size = 4)

# Correct: iq on x-axis, d_mail on y-axis
p_correct <- ggplot(members, aes(x = iq, y = d_mail)) +
  geom_point(size = 4)

ggsave("wrong.png",   plot = p_wrong,   width = 5, height = 4)
ggsave("correct.png", plot = p_correct, width = 5, height = 4)
cat("Compare the two files to see the swapped axes\n")
Compare the two files to see the swapped axes

Mistake 2: Using geom_bar() without stat on pre-aggregated data

geom_bar() defaults to stat = "count", which counts rows to determine bar height. When you want bar height to reflect an existing y-column value, use geom_col() or specify stat = "identity" explicitly. Passing pre-aggregated data to plain geom_bar() produces an error when a y aesthetic is also provided.

ggplot2_geombar_mistake.R
library(ggplot2)

# Pre-aggregated data (iq column should become bar height directly)
members <- data.frame(
  name = c("Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri", "Hashida Itaru", "Kiryu Moeka"),
  iq   = c(128, 170, 95, 145, 138)
)

# Wrong: geom_bar() with a y aesthetic raises an error
# ggplot(members, aes(x = name, y = iq)) + geom_bar()
# Error: stat_count() can only have an x or y aesthetic.

# Option 1: use geom_col() (y value becomes bar height directly)
p1 <- ggplot(members, aes(x = name, y = iq)) +
  geom_col()

# Option 2: explicitly specify stat = "identity"
p2 <- ggplot(members, aes(x = name, y = iq)) +
  geom_bar(stat = "identity")

ggsave("geom_col.png", plot = p1, width = 5, height = 4)
ggsave("geom_bar.png", plot = p2, width = 5, height = 4)
cat("geom_col() and geom_bar(stat='identity') produce identical graphs\n")
geom_col() and geom_bar(stat='identity') produce identical graphs

Mistake 3: Omitting the file extension in ggsave()

ggsave() determines the output format from the extension in filename. Saving without an extension results in an error. Always include the desired format extension — PNG, PDF, SVG, and so on.

ggplot2_ggsave_mistake.R
library(ggplot2)

members <- data.frame(
  name = c("Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri"),
  iq   = c(128, 170, 95)
)

p <- ggplot(members, aes(x = name, y = iq)) + geom_col()

# Wrong: no extension
# ggsave("output", plot = p)
# Error in check_device(filename, ...) : Unknown graphics device ''

# Correct: include a format extension (PNG, PDF, SVG, etc.)
ggsave("output.png", plot = p, width = 5, height = 4, dpi = 150)
cat("Saved to output.png\n")
Saved to output.png

Overview

ggplot2 graphs are characterized by a layered structure: start with ggplot(data, aes(...)) to create the canvas, then stack layers using the + operator. aes() (aesthetic mapping) binds data columns to visual properties such as x, y, color, fill, and size. The geom_*() functions use that information to draw shapes — points, bars, lines, and more. The aes() passed to ggplot() is inherited by all layers, but individual geom_*() calls can receive their own aes() to override or extend the mapping per layer. labs() sets titles and axis labels, while theme_*() controls the overall appearance of the panel background, gridlines, and fonts. A completed graph object can be exported to PNG, PDF, SVG, and other formats with ggsave(). For data preparation, see Data Frame Basics. For displaying multiple graphs together, see Merging Data Frames.

If you find any errors or copyright issues, please .