bhylu.com

Advertisements

R Multiple Plots

Keep related panels on one owned device with their scales labelled.

How it works

par controls graphics parameters for the current device. A multi-panel layout can support comparison, but inconsistent scales or unexplained units can mislead. This example places a count series beside a simple frequency plot, then restores its layout parameters before closing the owned PNG device.

Run this R example

These examples run locally with R, not PHP, Python or JavaScript. The reviewed baseline is R 4.5.2 on Windows x64/UCRT, using only packages shipped with R. This is a tested version, not a newest-release claim. Install your own R from the official R project; an IDE is optional. Save the complete source as multiple-plots.R in a separate learning folder and run Rscript --vanilla multiple-plots.R. Keep code and fictional fixtures separate from a real project.

Bhylu has no R browser runner. There is no Try in playground link because the existing Python and JavaScript editors do not execute R. No package installation, account, network dataset, Apache, MySQL or PHP setting is needed to run these base-R sources.

The complete download uses a local scope, restores its temporary options and treats unexpected warnings as errors. Examples that handle an expected warning do so narrowly. Random examples change only their R process’s RNG state. File examples own their temporary output; figure examples leave their named PNG in the current learning folder and refuse to overwrite it. Use a new empty folder to rerun a figure.

Download the complete multiple-plots.R · Download all source files with hash checks

For the optional collection downloader, save download-examples.mjs in an empty folder and run node download-examples.mjs. Local preview before publishing uses node download-examples.mjs http://127.0.0.1:8000/examples/r/. It refuses an existing bhylu-r-examples folder and does not install or execute R.

Example

multiple-plots.R

local({
  previous_options <- options(warn = 2, digits = 8, stringsAsFactors = FALSE)
  on.exit(options(previous_options), add = TRUE)
  path <- "multiple-plots.png"
  if (file.exists(path)) stop("Refuse an existing figure file")
  png(path, width = 900, height = 600, res = 120, type = "cairo")
  graphics_device <- dev.cur()
  on.exit({
    if (graphics_device %in% dev.list()) invisible(dev.off(graphics_device))
  }, add = TRUE)
  old <- par(mfrow = c(1, 2), mar = c(4, 4, 3, 1))
  counts <- c(8, 12, 10, 14)
  plot(seq_along(counts), counts, type = "b", pch = 19, col = "#0078d4",
    xlab = "Session", ylab = "Visits", ylim = c(0, 16), main = "Session order")
  barplot(table(counts), col = "#0f3464", xlab = "Visit count",
    ylab = "Frequency", main = "Observed frequency")
  par(old)
  invisible(dev.off(graphics_device))
  cat("Saved multiple-plots.png\n")
  invisible(NULL)
})

Focus on the lesson operations

This excerpt omits the complete script’s option/resource setup. Use the full download to execute it.

Operation excerpt

old <- par(mfrow = c(1, 2), mar = c(4, 4, 3, 1))
counts <- c(8, 12, 10, 14)
plot(seq_along(counts), counts, type = "b", pch = 19, col = "#0078d4",
  xlab = "Session", ylab = "Visits", ylim = c(0, 16), main = "Session order")
barplot(table(counts), col = "#0f3464", xlab = "Visit count",
  ylab = "Frequency", main = "Observed frequency")
par(old)

Expected result

Saved multiple-plots.png

The left panel preserves order, while the right asks how often each value occurred. Their x axes have different meanings and are labelled accordingly. Closing a private graphics device ends its parameter lifetime without changing a learner’s unrelated interactive device.

Two labelled panels showing synthetic session order and count frequencies.
Two labelled panels showing synthetic session order and count frequencies. Generated by the reviewed R source, using fictional data.

For API behaviour, consult the official Introduction to R and your installed help pages. These small examples do not establish real-world statistical conclusions or production performance.

Advertisements