Data Visualization in R with ggplot2 | The School of Code

Settings

Appearance

Choose a typography theme that suits your style

Data Visualization in R with ggplot2

Master data visualization using ggplot2, the powerful and elegant R graphics library.

1

Introduction to ggplot2

ggplot2 is a powerful data visualization package based on the Grammar of Graphics. It builds plots by combining layers: data, aesthetic mappings (using aes()), and geometric objects (like geom_point()).

Example

Run this example to see how it works and then try the exercise.

example.r
Loading...
Output
Click "Run" to see the example in action...
Example

Run this example to see how it works and then try the exercise.

example.r
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: Create a data frame with x and y values

main.r
Loading...
Output
Click "Run" to execute your code...
Quiz

What function defines the aesthetic mappings in ggplot2?

SUMMARY

You learned the basic structure of ggplot2: data + aesthetics + geometry. Next, you’ll create scatter plots with more customization.


2

Scatter Plots

Scatter plots display relationships between two continuous variables. Use geom_point() to create them. You can customize color, size, shape, and transparency either as fixed values or mapped to data variables.

Example

Run this example to see how it works and then try the exercise.

example.r
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: Create a scatter plot using the mtcars dataset

main.r
Loading...
Output
Click "Run" to execute your code...
Quiz

How do you add a linear trend line to a scatter plot?

SUMMARY

You learned to create and customize scatter plots. You can set fixed aesthetics or map them to data variables. Next, you’ll learn about line graphs.


3

Line Graphs

Line graphs show trends over time or ordered categories. Use geom_line() to connect points in order. Combine with geom_point() to mark data points. For multiple lines, map color to a grouping variable.

Example

Run this example to see how it works and then try the exercise.

example.r
Loading...
Output
Click "Run" to see the example in action...
Exercise

Your task: Create time series data

main.r
Loading...
Output
Click "Run" to execute your code...
Quiz

What aesthetic is used to create multiple lines for different groups?

SUMMARY

You learned to create line graphs for time series data. Use color or group aesthetics for multiple lines. Next, you’ll learn about bar charts.

Unlock 11 More Lessons

You've completed the free preview! Upgrade to Pro to access all 13 lessons in this course, plus advanced projects and certificates.

Locked lessons:
  • Bar Charts
  • Histograms and Density Plots
  • Box Plots and Violin Plots
  • Customizing Aesthetics
  • Faceting
  • Themes
  • Labels and Annotations
  • Scales and Axes
  • Combining and Saving Plots
  • Conclusion

Reset Progress?

Are you sure you want to reset all progress for this course? This action cannot be undone.