R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
23: Introduction to ggplot2
Imagine you're an artist preparing to paint a landscape. You don't just start throwing paint at a wall. First, you get your canvas ready. Then, you sketch out where the horizon line goes and where the mountains will sit—this is your layout, but there's no "art" on it yet. Finally, you start layering: a wash of blue for the sky, some green jagged shapes for the hills, and maybe some tiny white dots for distant sheep. Each layer adds a new dimension to the image without erasing what you did before.
This is exactly how ggplot2 works. It's based on the "Grammar of Graphics," which is a fancy way of saying that every plot is just a series of layers stacked on top of each other.
Setting the Stage with ggplot()
In the R world, the ggplot() function is your blank canvas. By itself, it does absolutely nothing. If you run ggplot(data = my_data), R will just give you a grey void. That's because you've told it which dataset to use, but you haven't told it where the "horizon" is.
To fix that, we use aes(), short for aesthetics. This is where you map your data variables to visual properties. Think of it as the sketch phase: "I want the X-axis to represent time and the Y-axis to represent revenue."
# Let's imagine we have a dataset of coffee sales
coffee_sales <- data.frame(
day = 1:10,
revenue = c(120, 150, 110, 180, 200, 170, 210, 250, 230, 280)
)
# This sets the canvas and the sketch, but still shows nothing!
ggplot(data = coffee_sales, aes(x = day, y = revenue))
Choosing Your Visual Layer
Now that the canvas is prepped, we need to actually "paint" the data. We do this using geom functions (short for geometric objects). Depending on what you want to see, you'll pick a different geom. If you want a scatter plot, you use geom_point(). If you want a line chart, you use geom_line().
I've seen a lot of beginners try to put the aes() inside the geom function. You can do that, but it's usually better to keep the global mappings in the main ggplot() call so all your layers share the same coordinate system. It just keeps your code cleaner.
# Adding the 'paint' to our canvas
ggplot(data = coffee_sales, aes(x = day, y = revenue)) +
geom_point() +
geom_line()
The Magic of the Plus Sign
You probably noticed that + symbol. In most of R, + is for math. In ggplot2, it's the "layering" operator. It tells R, "Take the plot I just defined, and then add this next thing on top of it."
This is where ggplot2 becomes incredibly powerful. You can keep stacking. Want to change the labels? Add labs(). Want to change the theme to something professional? Add theme_minimal(). It's like adding a frame to your painting and signing your name at the bottom.
ggplot(data = coffee_sales, aes(x = day, y = revenue)) +
geom_point(color = "steelblue", size = 3) +
geom_line(color = "darkgrey") +
labs(title = "Daily Coffee Revenue",
x = "Day of Month",
y = "Total Revenue ($)") +
theme_minimal()
One quick tip: if your plot looks "off," check your parentheses. A common frustration is putting the + at the start of a new line instead of the end of the previous one. R reads code line-by-line; if the line ends without a +, R thinks the plot is finished and will ignore everything that follows.
📋 Practical Task
Visualizing Monthly Temperature Fluctuations in London
You've been given a small dataset representing the average monthly temperatures in London. Your goal is to create a professional-looking visualization that shows the trend over the year.
The Data:
london_temps <- data.frame( month = factor(month = 1:12, levels = 1:12), temp = c(5, 6, 9, 12, 15, 18, 21, 20, 17, 13, 9, 6) )Your Requirements:
- Initialize the plot using
london_temps, mappingmonthto the x-axis andtempto the y-axis. - Add a layer of points to show the individual monthly data.
- Add a layer of lines to connect those points.
- Change the color of the points to "red" and the line to "blue".
- Add a clear title: "Average Monthly Temperature in London" and label the axes as "Month" and "Temperature (°C)".
- Apply
theme_light()to finish the look.
There are no comments for now.