start a new graph while keeping the old ones visible with R

start a new graph while keeping the old ones visible with R

To start a new graph while keeping the old ones visible with RTo start a new graph while keeping the old ones visible, you can use the par() function in R to manage the graphical parameters. Here’s how you can handle multiple plots in the same plotting area:

Using par(mfrow=…) for Multiple Plots in One Window

The par(mfrow=…) function allows you to arrange multiple plots in a single plotting window. This is useful for comparing different plots side by side or in a grid.

Example: 

x <- 1:10
y <- x^2
z <- x^3
# Set up a 2x2 plotting area
par(mfrow = c(2, 2))
# Plot 1
plot(x, y, main = "Plot 1: x vs x^2")
# Plot 2
plot(x, z, main = "Plot 2: x vs x^3")
# Plot 3
hist(y, main = "Plot 3: Histogram of x^2")
# Plot 4
boxplot(x, y, main = "Plot 4: Boxplot")
  • par(mfrow = c(2, 2)): Divides the plotting window into a 2×2 grid.

Using par(mfcol=…) for Filling by Column

Similar to mfrow, mfcol fills plots column-wise instead of row-wise.

Example: 

# Set up a 2x2 plotting area, filling by column
par(mfcol = c(2, 2))
# Plot 1
plot(x, y, main = "Plot 1: x vs x^2")
# Plot 2
plot(x, z, main = "Plot 2: x vs x^3")
# Plot 3
hist(y, main = "Plot 3: Histogram of x^2")
# Plot 4
boxplot(x, y, main = "Plot 4: Boxplot")

Adding New Plots to the Same Graph

If you want to add a new plot to an existing one without clearing the previous plot, use the add = TRUE argument in plotting functions or just overlay plots.

Example: 

# Initial plot
plot(x, y, main = "Overlay Plot", xlab = "X", ylab = "Y", col = "blue")
# Add another plot on top
points(x, z, col = "red")
  • points(): Adds points to an existing plot.

Saving and Restoring Graphical Parameters

You might want to save the current graphical parameters and restore them later.

Example: 

# Save current parameters
old_par <- par()
# Set up a new plotting area
par(mfrow = c(1, 2))
# Create plots
plot(x, y, main = "Plot 1")
plot(x, z, main = "Plot 2")
# Restore old parameters
par(old_par)
  • old_par <- par(): Saves the current graphical parameters.
  • par(old_par): Restores the saved parameters.

Opening a New Graphics Device

To keep old plots while starting a new one, open a new graphics device using functions like windows(), quartz(), or x11() depending on your operating system.

Example: 

# Open a new graphics window
windows()  # Use quartz() on macOS or x11() on Linux
# Create a new plot in the new window
plot(x, y, main = "New Plot in New Window")
  • windows(), quartz(), x11(): Open new graphics devices.

Conclusion

These methods allow you to manage multiple plots effectively, whether you want to view them together in a single window or keep old plots visible while creating new ones. Experimenting with par() and different graphics devices will help you find the best approach for your needs.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print