📊 R – Charts & Graphs Overview: Bar, Line, Pie, Box, Scatter Plots
🧲 Introduction – Visualizing Data in R
R is renowned for its powerful data visualization capabilities. From simple bar charts to complex multi-faceted plots, R allows you to visualize trends, distributions, and relationships using both base R functions and advanced libraries like ggplot2
.
🎯 In this guide, you’ll learn:
- The most common types of charts and graphs in R
- Syntax using both base R and
ggplot2
- Practical code examples with clear explanations
- When to use each type of chart effectively
🧰 1. Bar Charts – Compare Categories
✅ Base R:
counts <- table(mtcars$cyl)
barplot(counts, main = "Cylinder Counts", xlab = "Cylinders", col = "steelblue")
🔍 Explanation:
table()
creates frequency counts ofcyl
.barplot()
visualizes the count by category.main
sets title,xlab
sets x-axis label.
✅ ggplot2:
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(fill = "skyblue") +
labs(title = "Cylinder Counts", x = "Cylinders", y = "Count")
📈 2. Line Charts – Trend Over Time
plot(AirPassengers, type = "l", col = "blue", lwd = 2,
main = "Monthly Airline Passengers", ylab = "Passengers")
🔍 Explanation:
type = "l"
specifies line chart.lwd
adjusts line width.- Useful for time series data.
📉 3. Scatterplots – Show Relationships
plot(mtcars$wt, mtcars$mpg,
main = "MPG vs Weight", xlab = "Weight", ylab = "MPG",
pch = 19, col = "red")
🔍 Explanation:
pch = 19
sets solid circle.- Reveals correlation between weight and fuel efficiency.
📊 4. Pie Charts – Part-to-Whole Comparison
slices <- c(10, 20, 30, 40)
labels <- c("A", "B", "C", "D")
pie(slices, labels = labels, main = "Pie Chart Example")
🔍 Explanation:
- Each slice shows percentage of total.
- Use sparingly (bar charts often more informative).
📦 5. Boxplots – Summary of Distributions
boxplot(mpg ~ cyl, data = mtcars,
main = "MPG by Cylinder", xlab = "Cylinders", ylab = "MPG")
🔍 Explanation:
- Compares distribution of
mpg
across cylinder groups. - Useful for spotting outliers and variability.
📚 6. Histograms – Frequency Distribution
hist(mtcars$mpg, breaks = 10, col = "gray",
main = "Histogram of MPG", xlab = "Miles Per Gallon")
🔍 Explanation:
breaks = 10
defines bin count.- Ideal for checking skewness, spread, and normality.
📐 7. Advanced Plotting with ggplot2
✅ Quick Overview:
Function | Description |
---|---|
geom_bar() | Bar chart |
geom_line() | Line plot |
geom_point() | Scatterplot |
geom_boxplot() | Boxplot |
geom_histogram() | Histogram |
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "blue", color = "white")
🖼️ Save Charts to File
png("myplot.png", width = 600, height = 400)
plot(mtcars$wt, mtcars$mpg)
dev.off()
🔍 Explanation:
png()
opens graphic device.dev.off()
saves and closes the plot.
📌 Summary – Recap & Next Steps
Charts and graphs are essential for communicating insights visually. R makes plotting fast, customizable, and powerful for any level of complexity.
🔍 Key Takeaways:
- Use base R (
plot()
,barplot()
,hist()
) for simple visuals - Use
ggplot2
for flexible and professional charts - Choose chart types based on your data goal (compare, trend, relate)
- Always label axes, use color meaningfully, and explore data visually
⚙️ Real-World Relevance:
Used in business dashboards, academic research, machine learning diagnostics, and automated reporting.
❓ FAQs – R Charts & Graphs
❓ What is the difference between plot()
and ggplot()
in R?
✅ plot()
is from base R—simple and quick. ggplot()
from ggplot2
is more customizable and elegant.
❓ Can I save a plot directly as a PDF or PNG?
✅ Yes. Use pdf()
, png()
, jpeg()
, and wrap your plot inside.
❓ How do I add a legend to a plot?
✅ Use legend()
in base R or labs(color = "Group")
in ggplot2.
❓ Which chart is best for showing part-to-whole relationships?
✅ Pie charts can show parts, but bar charts are more effective and accurate.
❓ How do I plot multiple lines on the same chart?
✅ Use lines()
in base R or multiple geom_line()
calls in ggplot2.
Share Now :