đ Introduction
Base Graphics is R's built-in graphics system used to create visual representations of data without requiring additional packages. It provides a wide range of plotting functions for creating charts such as scatter plots, line graphs, bar charts, histograms, pie charts, box plots, and more. Base Graphics is simple, fast, and ideal for exploratory data analysis and basic data visualization.
Information
đ¯ Why Learn Base Graphics?
- Visualize data effectively.
- Identify patterns and trends.
- Detect outliers and unusual observations.
- Create charts for reports and presentations.
- Perform exploratory data analysis.
đ Common Base Graphics Functions
| Function | Purpose |
|---|---|
| plot() | Creates scatter plots and line plots. |
| barplot() | Creates bar charts. |
| hist() | Creates histograms. |
| pie() | Creates pie charts. |
| boxplot() | Creates box plots. |
| pairs() | Creates scatter plot matrices. |
| curve() | Plots mathematical functions. |
đ Creating a Scatter Plot
The plot() function creates a scatter plot by default.
Scatter Plot
x <- c(1,2,3,4,5)
y <- c(2,5,4,8,7)
plot(
x,
y,
main = "Scatter Plot",
xlab = "X Values",
ylab = "Y Values",
col = "blue",
pch = 19
)đ Creating a Line Plot
Set the type argument to "l" to create a line graph.
Line Plot
months <- 1:6
sales <- c(
120,
150,
180,
170,
210,
230
)
plot(
months,
sales,
type = "l",
col = "red",
lwd = 2,
main = "Monthly Sales"
)đ Creating a Bar Chart
The barplot() function creates vertical or horizontal bar charts.
Bar Chart
marks <- c(85,92,78,88)
names(marks) <- c(
"Alice",
"Bob",
"Charlie",
"David"
)
barplot(
marks,
col = "skyblue",
main = "Student Marks",
xlab = "Students",
ylab = "Marks"
)đ Creating a Histogram
Histograms display the frequency distribution of numerical data.
Histogram
scores <- c(
65,70,72,75,80,
82,85,87,90,92,
95,98
)
hist(
scores,
col = "lightgreen",
main = "Score Distribution",
xlab = "Scores"
)đĨ§ Creating a Pie Chart
Pie charts show how each category contributes to the whole.
Pie Chart
sales <- c(
40,
30,
20,
10
)
labels <- c(
"Electronics",
"Clothing",
"Furniture",
"Books"
)
pie(
sales,
labels = labels,
col = c(
"red",
"blue",
"green",
"yellow"
),
main = "Sales by Category"
)đĻ Creating a Box Plot
Box plots summarize the distribution of data and help identify outliers.
Box Plot
marks <- c(
78,82,85,90,
88,95,91,79,
84,87
)
boxplot(
marks,
col = "orange",
main = "Student Marks"
)đ Scatter Plot Matrix
The pairs() function creates scatter plots for every pair of variables in a dataset.
Scatter Plot Matrix
pairs(
iris[1:4],
main = "Iris Dataset"
)đ Plotting Mathematical Functions
The curve() function plots mathematical expressions.
Function Plot
curve(
x^2,
from = -5,
to = 5,
col = "purple",
lwd = 2,
main = "y = x²"
)đ¨ Customizing Plots
Base Graphics provides numerous arguments for customizing the appearance of charts.
| Argument | Purpose |
|---|---|
| main | Chart title. |
| xlab | X-axis label. |
| ylab | Y-axis label. |
| col | Color. |
| pch | Point symbol. |
| lwd | Line width. |
| cex | Size of points or text. |
| type | Type of plot. |
đŧ Adding Titles and Labels
Customized Plot
x <- 1:5
y <- c(2,4,3,6,5)
plot(
x,
y,
main = "Sales Trend",
sub = "January to May",
xlab = "Month",
ylab = "Sales",
col = "blue",
pch = 16,
cex = 1.5
)đ Multiple Plots in One Window
Use par() to display multiple plots in a single graphics window.
Multiple Plots
par(mfrow = c(2,2))
hist(rnorm(100))
boxplot(rnorm(100))
plot(1:10)
barplot(c(5,8,3,6))đž Saving Plots
Graphs can be saved to image files such as PNG or PDF.
Saving a Plot
png("sales_plot.png")
plot(
1:5,
c(10,20,30,25,40)
)
dev.off()đ Real-World Example
A company wants to visualize monthly sales using a line chart.
Monthly Sales Visualization
months <- c(
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun"
)
sales <- c(
12000,
13500,
15000,
14800,
17000,
18200
)
plot(
sales,
type = "o",
xaxt = "n",
col = "darkgreen",
pch = 16,
lwd = 2,
xlab = "Month",
ylab = "Sales",
main = "Monthly Sales Report"
)
axis(
1,
at = 1:6,
labels = months
)đ Base Graphics Workflow
đ Common Chart Types
| Chart | Best Used For |
|---|---|
| Scatter Plot | Relationship between two variables. |
| Line Plot | Trends over time. |
| Bar Chart | Comparing categories. |
| Histogram | Distribution of numerical data. |
| Pie Chart | Parts of a whole. |
| Box Plot | Distribution and outlier detection. |
â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Using mismatched vector lengths | X and Y values must contain the same number of elements. | Ensure both vectors have equal lengths. |
| Missing axis labels or titles | Makes charts difficult to interpret. | Add descriptive labels using main, xlab, and ylab. |
| Choosing an inappropriate chart type | Can misrepresent the data. | Select a chart based on the nature of the data. |
| Overusing colors and symbols | Reduces readability. | Keep visualizations simple and consistent. |
đĄ Best Practices
- Choose the chart type that best represents the data.
- Always include meaningful titles and axis labels.
- Use colors consistently to improve readability.
- Avoid cluttering charts with unnecessary elements.
- Preview plots before exporting them for reports.
Best Practice
đ Summary
Base Graphics is R's built-in visualization system for creating a variety of charts without additional packages. You learned how to create scatter plots, line plots, bar charts, histograms, pie charts, box plots, scatter plot matrices, and mathematical function plots. You also explored plot customization, multiple plot layouts, and saving graphics to files. Mastering Base Graphics provides a strong foundation for exploring data and communicating analytical results effectively.