An Introduction to R's ggplot2 for Beautiful Visualizations
๐จ An Introduction to R's ggplot2 for Beautiful Visualizations
๐ฆ Install and Load ggplot2
install.packages("ggplot2") # Install once
library(ggplot2) # Load in each session
๐ Using a Sample Dataset
We’ll use the built-in mtcars and diamonds datasets:
data(mtcars)
head(mtcars)
data(diamonds)
head(diamonds)
๐ง The Grammar of Graphics
ggplot2 is built using layers:
ggplot(data) +
aes(mapping) +
geom_*() +
theme_*() +
labs()
Each component adds something to the plot.
๐น 1. Basic Scatter Plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
✅ Shows the relationship between weight (wt) and miles per gallon (mpg).
๐น 2. Add Color and Size
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl), size = hp)) +
geom_point()
✅ Use color for cylinder count and size for horsepower.
๐น 3. Add Trend Line (Linear Regression)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
✅ Adds a linear trend line without the confidence interval.
๐น 4. Bar Plot
ggplot(diamonds, aes(x = cut)) +
geom_bar(fill = "skyblue")
✅ Shows count of diamonds by cut.
๐น 5. Box Plot
ggplot(diamonds, aes(x = cut, y = price)) +
geom_boxplot(fill = "orange")
✅ Compare price distribution by cut quality.
๐น 6. Histogram
ggplot(diamonds, aes(x = price)) +
geom_histogram(binwidth = 500, fill = "steelblue")
✅ Visualize distribution of diamond prices.
๐น 7. Density Plot
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_density(alpha = 0.4)
✅ Smoothed distribution, grouped by cut.
๐น 8. Faceting: Multiple Plots by Category
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_wrap(~cyl)
✅ Creates separate scatter plots for each cylinder type.
๐น 9. Customizing Labels and Themes
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "darkgreen") +
labs(
title = "Fuel Efficiency by Car Weight",
x = "Weight (1000 lbs)",
y = "Miles per Gallon"
) +
theme_minimal()
✅ Improves readability and adds a clean theme.
๐น 10. Save Your Plot
ggsave("my_plot.png", width = 8, height = 6, dpi = 300)
✅ Save your chart as an image file.
๐ฏ Quick Reference: Common ggplot2 Geoms
Geom Description
geom_point() Scatter plots
geom_bar() Bar charts (categorical)
geom_col() Bar charts with values
geom_histogram() Histograms
geom_boxplot() Box plots
geom_line() Line plots
geom_density() Smoothed distributions
geom_smooth() Trend lines
๐ Pro Tips
Use theme_minimal(), theme_bw(), or theme_classic() to improve plot aesthetics.
Use aes() inside ggplot() to set global mappings.
Use aes() inside geom_*() for layer-specific mappings.
Always add labels and titles for clarity.
๐งช Mini Project Idea
Dataset: diamonds
Goals:
Bar plot of diamond cuts
Box plot of price by cut
Scatter plot of price vs. carat with color by clarity
Facet by cut or clarity
✅ Summary
ggplot2 allows you to:
Build plots layer-by-layer
Create stunning, professional-quality charts
Customize every detail with ease
Once you get comfortable with ggplot2, you’ll be able to communicate insights visually with far more impact.
Learn Data Science Course in Hyderabad
Read More
Visualizing Data with Matplotlib and Seaborn
Data Manipulation with dplyr in R
10 Pandas Functions Every Data Scientist Should Know
Focus on the practical, code-based aspects of data science.
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment