Data Visualization & Graphics in R
Estimated reading: 3 minutes 27 views

📈 R – Plot, Line Graphs & Scatterplots: Plot, Line Graph, and Scatter Examples Explained


🧲 Introduction – Visualizing Trends & Relationships in R

When analyzing numeric data, line charts and scatterplots are essential tools. R makes it easy to create both basic and advanced plots using built-in functions (plot(), lines()) and libraries like ggplot2.

🎯 In this guide, you’ll learn:

  • How to use base R and ggplot2 for creating line graphs and scatterplots
  • How to customize axes, colors, points, and titles
  • When to use each plot type for effective visual analysis

🔹 1. Basic plot() Function in R

The versatile plot() function can create scatterplots, line plots, and more based on its arguments.

x <- 1:10
y <- x^2
plot(x, y, main = "Basic Plot", xlab = "X-Axis", ylab = "Y-Axis")

🔍 Explanation:

  • x, y: Data points
  • main: Plot title
  • xlab, ylab: Axis labels
  • Since no type is specified, it defaults to a scatterplot

🔹 2. Scatterplots in Base R

plot(mtcars$wt, mtcars$mpg,
     main = "MPG vs Weight",
     xlab = "Weight (1000 lbs)",
     ylab = "Miles per Gallon",
     pch = 19, col = "blue")

🔍 Explanation:

  • pch = 19: Solid circle
  • col: Point color
  • Shows the inverse relationship between weight and MPG

🔹 3. Line Graph in Base R

plot(AirPassengers, type = "l", col = "red",
     main = "Air Passenger Traffic", ylab = "Passengers", xlab = "Time")

🔍 Explanation:

  • type = "l": Creates a line plot
  • AirPassengers: Built-in time series dataset

🔹 4. Adding Multiple Lines with lines()

x <- 1:10
y1 <- x^2
y2 <- x^1.5

plot(x, y1, type = "l", col = "blue", ylim = c(0, 100), ylab = "Values")
lines(x, y2, col = "green", lty = 2)
legend("topleft", legend = c("x^2", "x^1.5"), col = c("blue", "green"), lty = 1:2)

🔍 Explanation:

  • lines() adds extra lines to existing plot
  • lty changes line type (dashed, dotted, etc.)
  • legend() helps distinguish lines

🔷 5. Scatterplots with ggplot2

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "darkred", size = 3) +
  labs(title = "Fuel Efficiency by Car Weight", x = "Weight", y = "MPG")

🔍 Explanation:

  • geom_point(): Adds scatter points
  • aes(): Maps variables to axes
  • Highly customizable and clean design

🔷 6. Line Plots with ggplot2

df <- data.frame(
  month = 1:12,
  sales = c(100, 120, 130, 150, 160, 170, 165, 180, 190, 200, 220, 250)
)

ggplot(df, aes(x = month, y = sales)) +
  geom_line(color = "blue", size = 1.5) +
  labs(title = "Monthly Sales", x = "Month", y = "Sales")

🔍 Explanation:

  • geom_line() creates a line graph
  • Works best with time or continuous sequences

🧠 Plot Types at a Glance

TypeFunction (Base R)Function (ggplot2)
Scatterplotplot(type="p")geom_point()
Line Graphplot(type="l")geom_line()
Add Linelines()Additional geom_line()

📌 Summary – Recap & Next Steps

R provides powerful tools for trend and relationship visualization through plots. Whether using base R or ggplot2, you can clearly illustrate data patterns and insights.

🔍 Key Takeaways:

  • Use plot() for flexible scatter or line plotting
  • Add lines with type = "l" or lines()
  • Use ggplot2 for layered, customized, and professional visuals
  • Always label your axes and use legends for clarity

⚙️ Real-World Relevance:
Line and scatter plots are crucial in exploratory data analysis, time series modeling, regression diagnostics, and scientific visualization.


❓ FAQs – Plot, Line & Scatterplots in R

❓ How do I change point shapes in a scatterplot?
✅ Use pch = 1 to 25 in base R or shape in ggplot2:

plot(x, y, pch = 4)  # Cross

❓ Can I plot lines and points together in base R?
✅ Yes:

plot(x, y, type = "b")  # Both lines and points

❓ How do I control axis limits in plot()?
✅ Use xlim and ylim:

plot(x, y, xlim = c(0, 10), ylim = c(0, 100))

❓ Which is better: base R or ggplot2?
✅ Base R is faster for quick plots; ggplot2 is better for advanced, publication-quality graphics.

❓ How do I save my plot as an image?
✅ Use:

png("output.png")
plot(x, y)
dev.off()

Share Now :

Leave a Reply

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

Share

R – Plot / Line / Scatterplot

Or Copy Link

CONTENTS
Scroll to Top