ggplot2 Scales

Data Visualizationggplot2Free Lesson

Advertisement

Introduction

Scales control how data values are mapped to visual properties. They customize axis and legend displays.

Axis Scales

# Log scale
ggplot(df, aes(x, y)) + geom_point() +
  scale_y_log10()

# Reverse axis
ggplot(df, aes(x, y)) + geom_point() +
  scale_y_reverse()

# Custom breaks
ggplot(df, aes(x, y)) + geom_point() +
  scale_x_continuous(breaks = c(0, 5, 10, 15))

Color Scales

# Manual colors
ggplot(df, aes(x, y, color = category)) + geom_point() +
  scale_color_manual(values = c("A" = "red", "B" = "blue"))

# Gradient
ggplot(df, aes(x, y, color = value)) + geom_point() +
  scale_color_gradient(low = "blue", high = "red")

# Gradient2
ggplot(df, aes(x, y, color = value)) + geom_point() +
  scale_color_gradient2(low = "blue", mid = "white", high = "red")

Date Scales

ggplot(df, aes(x = date, y = value)) + geom_line() +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%b %Y")

Summary

Scales provide fine control over visualization appearance. Customize for clarity and aesthetics.

Advertisement

Need Expert R Programming Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement