Why ggplot2?
ggplot2 (by Hadley Wickham) is based on the “Grammar of Graphics” approach. The goal: a consistent system in which you:
- Define the dataset and aesthetics (x, y, color, shape …),
- Then add “geoms” (geom_point, geom_bar, geom_histogram, etc.),
- And expand it step by step, for example with themes, facets, and so on.
The result is often professional-looking graphics with little code, while remaining highly flexible.
(One small drawback: it takes some getting used to at first. But it’s worth it!)
Installation & Basics
install.packages("ggplot2") # einmalig
library(ggplot2)
Example:
# Nehmen wir den eingebauten Datensatz "mpg" aus ggplot2
head(mpg)
# manufacturer model displ year cyl trans ...
# Bisschen reinschauen
str(mpg)
ggplot(data=mpg, aes(x=displ, y=hwy)) +
geom_point()
- ggplot(…) creates the “framework”
- aes(x=displ, y=hwy) says: X-axis = displ, Y-axis = hwy
- + geom_point(): Draw points
Geoms and Aesthetics
Geoms
- geom_point(): Points (scatterplot)
- geom_histogram(): Histogram
- geom_bar(): Bars
- geom_boxplot(): Boxplot
- geom_line(): Lines
Aesthetics (aes)
- x=… and y=…
- color=… (color based on a categorical variable)
- size=… (point size)
- fill=… (fill color for boxplots/bars)
Example: Colored points by “class”
ggplot(data=mpg, aes(x=displ, y=hwy, color=class)) +
geom_point()
Each car class (e.g., “suv”, “compact”) gets its own color.
Different Chart Types
Scatterplot
ggplot(data=mpg, aes(x=displ, y=hwy)) +
geom_point() +
labs(title="Streudiagramm displ vs hwy",
x="Hubraum",
y="Kraftstoffeffizienz")
- labs() for titles and axis labels.
Histogram
ggplot(mpg, aes(x=hwy)) +
geom_histogram(binwidth=2, fill="steelblue", color="white") +
labs(title="Histogramm der Kraftstoffeffizienz (hwy)")
- binwidth=2: Bin width in the histogram
Boxplot
ggplot(mpg, aes(x=class, y=hwy, fill=class)) +
geom_boxplot() +
theme_minimal() +
labs(title="Boxplot hwy nach Fahrzeugklasse")
- fill=class: Each box is colored differently.
- theme_minimal(): A different “theme”.
Bar chart
If you want to count a categorical variable, use geom_bar():
ggplot(mpg, aes(x=class)) +
geom_bar(fill="orange") +
labs(title="Anzahl Fahrzeuge pro Klasse")
Note: stat=“count” is applied internally here because you have not specified a y value.
Faceting, Themes & More Tricks
Faceting
Splits the plot into several panels:
ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point(aes(color=class)) +
facet_wrap(~class) +
labs(title="Faceting nach Klasse")
Creates several scatterplots, one class per panel.
Themes
- theme_bw()
- theme_minimal()
- theme_classic()
You can also create your own themes. Themes change the background, axis lines, fonts, and more.
Legends, Scales
For example, you can customize the color scale:
ggplot(mpg, aes(x=displ, y=hwy, color=class)) +
geom_point() +
scale_color_brewer(palette="Set1") +
labs(title="Mit eigener Farbpalette")
Summary
Now you know how to use ggplot2 to:
- Define data via ggplot(data=…, aes(…)),
- Add geoms such as geom_point() and geom_boxplot(),
- Use facets and themes,
- Customize colors, labels, and axes.
ggplot2 is THE standard in R for many people when it comes to beautiful, flexible graphics. It takes some time to get used to, but then you can achieve professional results—for research reports, presentations, or blogs.
