Mac系统下R语言plot.new()报错:attempt to plot on null device求助
Hey there! Let's work through this plotting error you're hitting on your Mac. That Error in plot.new() : attempt to plot on null device typically crops up when R can't connect to a valid graphics window to draw your plot. It's a common issue on macOS, and there are a few straightforward fixes to try:
Quick Fixes to Try First
Reset your graphics device
Sometimes the default Quartz graphics device gets stuck in a broken state. Run these commands to reset it:dev.off() # Close any hanging graphics devices dev.new() # Open a fresh graphics window plot(mlm.lob) # Attempt your plot againThis clears out any leftover device connections and starts fresh, which often resolves the issue immediately.
Manually launch the Quartz device
macOS relies on Quartz for R's graphics, and sometimes it doesn't initialize automatically. Explicitly start it before plotting:quartz() # Open a Quartz graphics window plot(mlm.lob)
If the Quick Fixes Don't Work
Check your RStudio pane (if using RStudio)
Make sure the Plot pane in RStudio is visible and not minimized. If the pane is hidden, R might not have a place to render the plot. You can reset RStudio's layout viaTools > Global Options > Appearance > Reset RStudio Layoutif needed.Test in base R
Try running your code in the base R app (the blue R icon in your Applications folder) instead of RStudio. If the plot works there, the issue is likely with RStudio—try updating RStudio to the latest version or reinstalling it.Update R and macOS
Outdated software can cause compatibility issues with graphics devices. Update R to the latest stable version, and make sure your macOS is up to date via System Settings > General > Software Update.
Why This Happens
The "null device" error means R doesn't have an active graphics window to draw into. On macOS, this can occur if the Quartz device fails to initialize, a previous plot window crashed and left the device in an invalid state, or your IDE (like RStudio) isn't properly configured to handle graphics output.
内容的提问来源于stack exchange,提问作者dylan roddy

