Introduction
Geoms are the visual elements (points, lines, bars) in ggplot2. Different geoms create different plot types.
Point Geom
ggplot(df, aes(x = var1, y = var2)) +
geom_point()
# With options
ggplot(df, aes(x = var1, y = var2)) +
geom_point(shape = 21,
fill = "blue",
size = 3,
alpha = 0.7)
Line Geom
# Line plot
ggplot(df, aes(x = x, y = y)) +
geom_line()
# Point and line
ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_line()
Bar Geoms
# Bar plot
ggplot(df, aes(x = category, y = value)) +
geom_bar(stat = "identity")
# Stacked bar
ggplot(df, aes(x = category, y = value, fill = type)) +
geom_bar(stat = "identity")
# Horizontal bar
ggplot(df, aes(x = value, y = category)) +
geom_bar(stat = "identity") +
coord_flip()
Summary Geoms
# Box plot
ggplot(df, aes(x = category, y = value)) +
geom_boxplot()
# Violin plot
ggplot(df, aes(x = category, y = value)) +
geom_violin()
# Error bars
ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_errorbar(aes(ymin = lower, ymax = upper))
Summary
Choose appropriate geoms for your data and visualization goals. Combine geoms for complex plots.