Plotting Concepts
Scatter Plot
Mapping X and Y
A scatter plot maps paired vectors to horizontal and vertical positions. The plot type controls whether points, lines, or both are drawn.
Program
Play the script to choose the plot type and watch the same x/y data feed a base R plot call.
scatter_plot_mapping.R
Replay: real traced execution (multi-file project)
x <- c(1, 2, 3, 4)
type_index <- 1
plot_type <- c("p", "l", "b")[type_index]
y <- x * 2
pdf(tempfile(fileext = ".pdf"))
plot(x, y, type = plot_type, main = "Growth")
invisible(dev.off())
summary <- paste(plot_type, tail(y, 1), sep = ":")
cat(summary, "\n", sep = "")
x <- c(1, 2, 3, 4)
type_index <- 2
plot_type <- c("p", "l", "b")[type_index]
y <- x * 2
pdf(tempfile(fileext = ".pdf"))
plot(x, y, type = plot_type, main = "Growth")
invisible(dev.off())
summary <- paste(plot_type, tail(y, 1), sep = ":")
cat(summary, "\n", sep = "")
x <- c(1, 2, 3, 4)
type_index <- 3
plot_type <- c("p", "l", "b")[type_index]
y <- x * 2
pdf(tempfile(fileext = ".pdf"))
plot(x, y, type = plot_type, main = "Growth")
invisible(dev.off())
summary <- paste(plot_type, tail(y, 1), sep = ":")
cat(summary, "\n", sep = "")
x ← 1, 2, 3, 4
1x <- c(1, 2, 3, 4)2type_index <- 1values this step1, 2, 3, 4xtype_index ← 1
1x <- c(1, 2, 3, 4)2type_index <- 13plot_type <- c("p", "l", "b")[type_index]values this step1type_indexplot_type ← p
2type_index <- 13plot_type <- c("p", "l", "b")[type_index]4y <- x * 2values this steppplot_type1type_indexy ← 2, 4, 6, 8
3plot_type <- c("p", "l", "b")[type_index]4y <- x * 25pdf(tempfile(fileext = ".pdf"))values this step2, 4, 6, 8y1, 2, 3, 4xgraphics device ← temporary PDF device
4y <- x * 25pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")values this steptemporary PDF devicegraphics deviceplot ← Growth scatter plot (points)
5pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())values this stepGrowth scatter plot (points)plot1, 2, 3, 4x2, 4, 6, 8ypplot_typegraphics device ← closed
6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")values this steptemporary PDF device → closedgraphics devicesummary ← p:8
7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")values this stepp:8summarypplot_type8tail(y, 1)cat(summary, " ", sep = "")
8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")outputp:8values this stepp:8summary
x ← 1, 2, 3, 4
1x <- c(1, 2, 3, 4)2type_index <- 2values this step1, 2, 3, 4xtype_index ← 2
1x <- c(1, 2, 3, 4)2type_index <- 23plot_type <- c("p", "l", "b")[type_index]values this step2type_indexplot_type ← l
2type_index <- 23plot_type <- c("p", "l", "b")[type_index]4y <- x * 2values this steplplot_type2type_indexy ← 2, 4, 6, 8
3plot_type <- c("p", "l", "b")[type_index]4y <- x * 25pdf(tempfile(fileext = ".pdf"))values this step2, 4, 6, 8y1, 2, 3, 4xgraphics device ← temporary PDF device
4y <- x * 25pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")values this steptemporary PDF devicegraphics deviceplot ← Growth scatter plot (lines)
5pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())values this stepGrowth scatter plot (lines)plot1, 2, 3, 4x2, 4, 6, 8ylplot_typegraphics device ← closed
6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")values this steptemporary PDF device → closedgraphics devicesummary ← l:8
7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")values this stepl:8summarylplot_type8tail(y, 1)cat(summary, " ", sep = "")
8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")outputl:8values this stepl:8summary
x ← 1, 2, 3, 4
1x <- c(1, 2, 3, 4)2type_index <- 3values this step1, 2, 3, 4xtype_index ← 3
1x <- c(1, 2, 3, 4)2type_index <- 33plot_type <- c("p", "l", "b")[type_index]values this step3type_indexplot_type ← b
2type_index <- 33plot_type <- c("p", "l", "b")[type_index]4y <- x * 2values this stepbplot_type3type_indexy ← 2, 4, 6, 8
3plot_type <- c("p", "l", "b")[type_index]4y <- x * 25pdf(tempfile(fileext = ".pdf"))values this step2, 4, 6, 8y1, 2, 3, 4xgraphics device ← temporary PDF device
4y <- x * 25pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")values this steptemporary PDF devicegraphics deviceplot ← Growth scatter plot (points and lines)
5pdf(tempfile(fileext = ".pdf"))6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())values this stepGrowth scatter plot (points and lines)plot1, 2, 3, 4x2, 4, 6, 8ybplot_typegraphics device ← closed
6plot(x, y, type = plot_type, main = "Growth")7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")values this steptemporary PDF device → closedgraphics devicesummary ← b:8
7invisible(dev.off())8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")values this stepb:8summarybplot_type8tail(y, 1)cat(summary, " ", sep = "")
8summary <- paste(plot_type, tail(y, 1), sep = ":")9cat(summary, "\n", sep = "")outputb:8values this stepb:8summary
plot
`plot(x, y, type = ...)` draws paired x/y values with the requested geometry.
type
`type = "p"`, `"l"`, or `"b"` chooses points, lines, or both.
device
A graphics device is where R sends drawing commands; here a temporary PDF device keeps the example deterministic.