Grammar of Graphics concept in R Programming - Time & Space Complexity
When we create graphics using the Grammar of Graphics in R, we want to know how the time to draw changes as we add more data points.
We ask: How does the drawing time grow when the data size grows?
Analyze the time complexity of the following R code using ggplot2.
library(ggplot2)
n <- 1000
data <- data.frame(x = 1:n, y = rnorm(n))
ggplot(data, aes(x = x, y = y)) +
geom_point() +
geom_smooth()
This code creates a scatter plot with points and a smooth line for n data points.
Look at what repeats as n grows.
- Primary operation: Plotting each point and calculating the smooth line.
- How many times: Points plotted once each (n times), smoothing involves calculations over all points.
As n grows, the time to plot points grows linearly, but smoothing calculations are more complex.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 point plots + smoothing on 10 points |
| 100 | About 100 point plots + smoothing on 100 points |
| 1000 | About 1000 point plots + smoothing on 1000 points |
Pattern observation: Plotting points grows directly with n, smoothing calculations grow more than linearly but less than n squared.
Time Complexity: O(n)
This means the time to create the plot grows roughly in direct proportion to the number of data points.
[X] Wrong: "Adding more points does not affect plot time much because the computer is fast."
[OK] Correct: Each point requires drawing and calculations, so more points mean more work and longer time.
Understanding how plotting time grows helps you explain performance when working with data visualizations in real projects.
"What if we replaced geom_point() with geom_hex() for hexagonal binning? How would the time complexity change?"