ggplot2 Polish

Back

Loading concept...

ggplot2 Polish: Making Your Charts Shine ✨

The Art Gallery Analogy 🖼️

Imagine you’re an artist who just finished painting a beautiful picture. The painting itself is wonderful, but before showing it in a gallery, you need to:

  1. Pick the right frame (scales)
  2. Add a title card (labels)
  3. Make copies to share (saving)

That’s exactly what ggplot2 Polish is about! You’ve already learned to create plots. Now let’s make them gallery-ready.


1. ggplot Scales: Picking the Perfect Frame 🎨

What Are Scales?

Scales are like rulers and color palettes that control how your data appears. They translate your data into visual properties.

Simple Example:

  • Your data says “temperature = 100”
  • The scale decides: “100 should appear at THIS position on the y-axis”
  • The scale also decides: “And it should be THIS shade of red”

The Scale Family

# Basic pattern
scale_[aesthetic]_[type]()

# Examples:
scale_x_continuous()
scale_y_log10()
scale_color_manual()
scale_fill_gradient()

Continuous Scales: Numbers on a Number Line

For data that flows smoothly (like height, weight, temperature):

ggplot(data, aes(x = age, y = height)) +
  geom_point() +
  scale_x_continuous(
    limits = c(0, 100),
    breaks = seq(0, 100, 20)
  ) +
  scale_y_continuous(
    limits = c(50, 200),
    breaks = seq(50, 200, 25)
  )

What this does:

  • limits: Set the min and max shown
  • breaks: Where to put tick marks

Log Scales: When Numbers Get Huge

Sometimes your data spans from 1 to 1,000,000. Regular scales make small values invisible!

# Population of cities (tiny to massive)
ggplot(cities, aes(x = name, y = pop)) +
  geom_col() +
  scale_y_log10()  # Now we can see all bars!

Think of it like a zoom lens that shows both ants and elephants clearly.

Color Scales: Painting with Data

# Gradient for continuous data
ggplot(data, aes(x, y, color = temp)) +
  geom_point() +
  scale_color_gradient(
    low = "blue",
    high = "red"
  )

# Manual colors for categories
ggplot(data, aes(x, y, color = group)) +
  geom_point() +
  scale_color_manual(
    values = c("A" = "#FF6B6B",
               "B" = "#4ECDC4",
               "C" = "#45B7D1")
  )

Fill vs Color: What’s the Difference?

  • Color = outline/border of shapes
  • Fill = inside of shapes
# Bars use FILL (inside color)
scale_fill_gradient()

# Points use COLOR (the dot itself)
scale_color_gradient()

2. ggplot Labels: Your Chart’s Name Tag 🏷️

Why Labels Matter

A chart without labels is like a book without a title. Nobody knows what they’re looking at!

The labs() Function: Your Labeling Toolbox

ggplot(data, aes(x = age, y = income)) +
  geom_point() +
  labs(
    title = "Income Grows with Age",
    subtitle = "Based on 2024 survey data",
    x = "Age (years)",
    y = "Annual Income ($)",
    caption = "Source: National Survey",
    color = "Education Level"
  )

Quick Label Shortcuts

# Just change one thing at a time
ggtitle("My Awesome Chart")
xlab("Time (seconds)")
ylab("Speed (km/h)")

Making Labels Beautiful

ggplot(data, aes(x, y)) +
  geom_point() +
  labs(title = "My Title") +
  theme(
    plot.title = element_text(
      size = 16,
      face = "bold",
      hjust = 0.5  # Center it!
    ),
    axis.title = element_text(
      size = 12,
      color = "darkblue"
    )
  )

The Complete Label Recipe

graph TD A["labs function"] --> B["title: Main heading"] A --> C["subtitle: Extra info"] A --> D["x: X-axis name"] A --> E["y: Y-axis name"] A --> F["caption: Data source"] A --> G["color/fill: Legend title"]

3. Saving ggplot Plots: Share Your Art! 💾

The ggsave() Hero

ggsave() is your best friend for saving plots. It’s smart and simple!

# Create your plot
my_plot <- ggplot(data, aes(x, y)) +
  geom_point() +
  labs(title = "My Beautiful Chart")

# Save it!
ggsave("my_chart.png", my_plot)

Picking the Right Format

Format Best For Size
PNG Web, presentations Medium
PDF Print, papers Vector
SVG Web (scalable) Vector
JPEG Photos Small
# Different formats
ggsave("chart.png", my_plot)
ggsave("chart.pdf", my_plot)
ggsave("chart.svg", my_plot)

Controlling Size

ggsave(
  "chart.png",
  my_plot,
  width = 8,     # inches
  height = 6,    # inches
  dpi = 300      # dots per inch (quality)
)

Quick Guide:

  • Web: 72-150 dpi
  • Print: 300+ dpi
  • Poster: 600+ dpi

The Last Plot Trick

Didn’t save your plot to a variable? No problem!

# Just ran this...
ggplot(data, aes(x, y)) + geom_point()

# Save whatever was just displayed
ggsave("last_plot.png")

ggsave() automatically grabs the last plot you made!

Pro Tips for Saving

# For presentations (bigger text)
ggsave("presentation.png",
       width = 10, height = 6,
       dpi = 150)

# For publications (crisp details)
ggsave("publication.pdf",
       width = 7, height = 5)

# For social media (square)
ggsave("instagram.png",
       width = 6, height = 6,
       dpi = 150)

Putting It All Together 🎯

Here’s a complete example with all three skills:

library(ggplot2)

# The data
sales <- data.frame(
  month = 1:12,
  revenue = c(100, 120, 150, 180,
              200, 250, 300, 280,
              260, 220, 180, 150)
)

# Create polished plot
final_plot <- ggplot(sales,
  aes(x = month, y = revenue)) +
  geom_line(color = "#4ECDC4",
            size = 1.5) +
  geom_point(color = "#FF6B6B",
             size = 3) +

  # SCALES
  scale_x_continuous(
    breaks = 1:12,
    labels = month.abb
  ) +
  scale_y_continuous(
    limits = c(0, 350),
    labels = scales::dollar
  ) +

  # LABELS
  labs(
    title = "2024 Revenue by Month",
    subtitle = "Strong summer performance",
    x = "Month",
    y = "Revenue",
    caption = "Data: Sales Team"
  )

# SAVE IT
ggsave("sales_report.png",
       final_plot,
       width = 8, height = 5,
       dpi = 300)

Quick Reference 📝

Scales

scale_x_continuous()  # Number x-axis
scale_y_log10()       # Log scale
scale_color_manual()  # Custom colors
scale_fill_gradient() # Color gradients

Labels

labs(title, subtitle, x, y, caption)
ggtitle("Title")
xlab("X Label")
ylab("Y Label")

Saving

ggsave("file.png", plot,
       width, height, dpi)

You Did It! 🎉

You now know how to:

  • Scale your axes and colors perfectly
  • Label your charts clearly
  • Save your work in any format

Your charts are no longer just data. They’re communication. They’re art. They’re ready to share with the world!

Go make something beautiful. 🌟

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.