Base Graphics in R

📘 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

Base Graphics is included with every R installation, so no additional packages are required to create plots.

đŸŽ¯ 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

FunctionPurpose
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.

ArgumentPurpose
mainChart title.
xlabX-axis label.
ylabY-axis label.
colColor.
pchPoint symbol.
lwdLine width.
cexSize of points or text.
typeType 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

Prepare Data
Select Chart Type
Create Plot
Customize Appearance
Add Titles and Labels
Display or Save Plot

📋 Common Chart Types

ChartBest Used For
Scatter PlotRelationship between two variables.
Line PlotTrends over time.
Bar ChartComparing categories.
HistogramDistribution of numerical data.
Pie ChartParts of a whole.
Box PlotDistribution and outlier detection.

âš ī¸ Common Mistakes

MistakeExplanationSolution
Using mismatched vector lengthsX and Y values must contain the same number of elements.Ensure both vectors have equal lengths.
Missing axis labels or titlesMakes charts difficult to interpret.Add descriptive labels using main, xlab, and ylab.
Choosing an inappropriate chart typeCan misrepresent the data.Select a chart based on the nature of the data.
Overusing colors and symbolsReduces 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

Effective visualizations communicate information clearly and accurately. Keep charts simple, label them properly, and focus on highlighting the most important insights from the data.

📝 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.

>>"A well-designed graph transforms numbers into insights, making data easier to understand and communicate."