R Base Plots

Base R includes a complete plotting system that requires no extra packages. With a single function call you can produce scatter plots, bar charts, histograms, pie charts, and line graphs. These plots are quick to create and ideal for fast exploratory analysis before you invest in polished visualizations.

The plot() Function — Universal Starting Point

# Scatter plot: two numeric vectors
x <- c(1, 2, 3, 4, 5, 6, 7)
y <- c(3, 5, 2, 8, 6, 9, 4)
plot(x, y)

# Add labels and title
plot(x, y,
     main   = "Score vs Study Hours",
     xlab   = "Study Hours",
     ylab   = "Score",
     col    = "steelblue",
     pch    = 16,          # solid circle point
     cex    = 1.5)         # point size

Common Plot Types

Function        Chart Type         Example Call
──────────────────────────────────────────────────────────────
plot(x, y)      Scatter plot       plot(hours, scores)
barplot(x)      Bar chart          barplot(sales)
hist(x)         Histogram          hist(ages)
boxplot(x)      Box plot           boxplot(scores ~ class)
pie(x)          Pie chart          pie(shares)
plot(x, type="l") Line chart      plot(months, sales, type="l")

Bar Chart

sales <- c(North=4200, South=5800, East=3600, West=4900)

barplot(sales,
        main   = "Regional Sales",
        xlab   = "Region",
        ylab   = "Sales (₹)",
        col    = c("tomato","steelblue","seagreen","orange"),
        border = "white")

Histogram

ages <- c(22,25,23,28,31,27,24,29,26,22,30,25,27,23,28)

hist(ages,
     main   = "Age Distribution",
     xlab   = "Age",
     ylab   = "Frequency",
     col    = "lightblue",
     border = "white",
     breaks = 5)          # number of bins

Line Chart

months  <- 1:12
revenue <- c(42,38,51,55,60,72,68,74,80,78,85,92)

plot(months, revenue,
     type  = "l",         # "l"=line, "b"=both, "o"=overplotted
     lwd   = 2,           # line width
     col   = "steelblue",
     main  = "Monthly Revenue 2024",
     xlab  = "Month",
     ylab  = "Revenue (₹ thousands)",
     xaxt  = "n")         # suppress default x-axis

axis(1, at=1:12, labels=month.abb)  # custom x-axis labels

Box Plot

scores_A <- c(72,85,90,88,78,92,65,80)
scores_B <- c(55,68,70,75,60,72,58,65)

boxplot(scores_A, scores_B,
        names  = c("Class A","Class B"),
        main   = "Score Distribution by Class",
        ylab   = "Score",
        col    = c("lightgreen","lightyellow"),
        border = "gray30")
Box plot anatomy:
  Max whisker ─── ┬
                  │
  75th %tile ─── ┌┴┐
                 │ │
  Median     ─── ├─┤ ◄ thick line
                 │ │
  25th %tile ─── └┬┘
                  │
  Min whisker ─── ┴
  ● = outlier

Pie Chart

market_share <- c(Android=72, iOS=26, Others=2)

pie(market_share,
    main   = "Mobile OS Market Share",
    col    = c("limegreen","dodgerblue","gray"),
    labels = paste0(names(market_share),"\n",market_share,"%"))

Multiple Plots in One Window

# par(mfrow) sets grid layout: rows × columns
par(mfrow = c(1, 2))   # 1 row, 2 columns

hist(scores_A, main="Class A", col="lightgreen")
hist(scores_B, main="Class B", col="lightyellow")

par(mfrow = c(1, 1))   # reset to single plot

Adding Lines and Points to Existing Plots

plot(months, revenue, type="l", col="steelblue", lwd=2)

# Add a horizontal reference line
abline(h=70, col="red", lty=2)   # dashed red line at y=70

# Add vertical line
abline(v=6, col="gray", lty=3)

# Add another data series
target <- rep(65, 12)
lines(months, target, col="orange", lwd=2, lty=2)

# Add a legend
legend("topleft", legend=c("Revenue","Target"),
       col=c("steelblue","orange"), lty=c(1,2), lwd=2)

Saving a Plot to File

png("my_chart.png", width=800, height=600)
plot(x, y, main="My Chart")
dev.off()   # close the device — file is saved

# Other formats
pdf("chart.pdf")
jpeg("chart.jpg", quality=90)
svg("chart.svg")

Base R plots are always available, require no installation, and run fast. They are the go-to tool for quick data checks during analysis. For publication-quality or interactive visualizations, ggplot2 (covered next) provides far more control over appearance.

Leave a Comment

Your email address will not be published. Required fields are marked *