如何在R中以真实毫米单位绘图并支持XY方向缩放?
Absolutely, this is totally doable in R—let’s walk through exactly how to pull this off, including the flexible X/Y axis scaling you need.
First: Match 1 Plot Unit to 1mm on A4
A4 paper has a physical size of 210mm (width) × 297mm (height). The trick is to align your R plot’s coordinate system directly to these dimensions, and make sure the output device matches the A4 size perfectly.
Using Base R
First, convert A4’s mm dimensions to inches (since R’s graphic devices default to inches for size):
- 210mm = ~8.27 inches
- 297mm = ~11.69 inches
Then, set up your plot with zero margins (so the entire paper is your plotting area) and set axis limits to match the mm dimensions. Here’s a complete example:
# Open an A4-sized PDF device (ideal for printing) pdf("a4_precise_plot.pdf", width = 210/25.4, height = 297/25.4) # Remove all margins so the plot fills the entire A4 sheet par(mai = c(0, 0, 0, 0)) # Initialize plot: X=0-210 (0-210mm), Y=0-297 (0-297mm) plot(0, 0, xlim = c(0, 210), ylim = c(0, 297), type = "n", axes = FALSE, xlab = "", ylab = "") # Add a diagonal line to verify it spans the full A4 sheet lines(c(0, 210), c(0, 297), lwd = 2) # Optional: Add axes with mm labels axis(1, at = seq(0, 210, 10), labels = seq(0, 210, 10)) axis(2, at = seq(0, 297, 10), labels = seq(0, 297, 10)) # Close the device dev.off()
In this setup, every unit on the X/Y axes directly maps to 1mm on the printed A4 paper.
Using ggplot2
If you prefer ggplot2, it’s even simpler—you can specify dimensions directly in mm when saving:
library(ggplot2) # Create a precise 1:1 mm plot ggplot() + geom_segment(aes(x = 0, y = 0, xend = 210, yend = 297), lwd = 2) + xlim(0, 210) + ylim(0, 297) + theme_void() + # Remove default plot elements theme(plot.margin = margin(0, 0, 0, 0, "mm")) + # Zero margins ggsave("a4_ggplot_precise.pdf", width = 210, height = 297, units = "mm")
Second: Scaling X/Y Axes
Scaling your axes just means adjusting the relationship between plot units and physical mm. You have two easy ways to do this:
1. Adjust Axis Limits
- Scale X axis to 2mm per plot unit: Set
xlim = c(0, 105)(since 105 units × 2mm = 210mm, filling the A4 width) - Scale Y axis to 0.5mm per plot unit: Set
ylim = c(0, 594)(594 units × 0.5mm = 297mm, filling the A4 height)
Example of X-axis scaling in base R:
pdf("a4_x_scaled_plot.pdf", width = 210/25.4, height = 297/25.4) par(mai = c(0, 0, 0, 0)) # X axis: 1 unit = 2mm (0-105 units = 0-210mm) plot(0, 0, xlim = c(0, 105), ylim = c(0, 297), type = "n", axes = FALSE, xlab = "", ylab = "") lines(c(0, 105), c(0, 297), lwd = 2, col = "red") # Label axes with actual mm values axis(1, at = seq(0, 105, 5), labels = seq(0, 210, 10)) axis(2, at = seq(0, 297, 10), labels = seq(0, 297, 10)) dev.off()
2. Adjust Plot Area Dimensions (Base R)
You can also use the par(pin) parameter to set the physical width/height of the plotting area (in inches). For example, halving the plot’s width will effectively double the X-axis scale:
pdf("a4_x_scaled_pin.pdf", width = 210/25.4, height = 297/25.4) par(mai = c(0, 0, 0, 0), pin = c((210/25.4)/2, 297/25.4)) # Half the X width plot(0, 0, xlim = c(0, 210), ylim = c(0, 297), type = "n", axes = FALSE, xlab = "", ylab = "") lines(c(0, 210), c(0, 297), lwd = 2, col = "blue") dev.off()
Key Takeaways
- To get 1 plot unit = 1mm, align your axis limits to A4’s mm dimensions and set the output device to match A4’s size.
- Scaling axes is as simple as adjusting axis limits (to change unit:mm ratio) or modifying the plot area’s physical dimensions.
内容的提问来源于stack exchange,提问作者Krisselack

