为何使用ggsave保存ggplot图形时文本会变大?如何使RStudio内联预览与保存结果一致?
When switching from {showtext} to {ragg}, the core issue causing mismatched preview and saved plots is that RStudio's default inline preview uses a 96dpi device (usually the base png() or system-specific device), while your ggsave call uses ragg::agg_png at 600dpi. ragg's font rendering is tightly tied to the device's dpi, so different dpi settings lead to inconsistent text scaling and visual output.
Here's how to align your inline preview and saved plot perfectly:
1. Set RStudio's Inline Preview to Use ragg with Matching DPI
You need to force RStudio's preview pane to use the ragg device with the same dpi you use for saving. You can do this either temporarily in your script or permanently in RStudio settings.
Temporary Script-Specific Setup
Add this line at the top of your RMarkdown script or R script:
# Configure RStudio's inline preview to use ragg at 600dpi (matches ggsave) options(device = function(...) ragg::agg_tk(device = "png", dpi = 600, ...))
This sets the preview device to ragg with the exact dpi you're using in ggsave, ensuring identical rendering logic.
Permanent RStudio Global Setup
If you want this to apply to all your RStudio sessions:
- Go to
Tools > Global Options > General > Graphics - Under Backend, select
AGG(this is ragg's device) - Set DPI to
600(or whatever dpi you use inggsave) - Click OK and restart RStudio for changes to take effect
2. Ensure Full Parameter Matching Between Preview and GGSave
Double-check that all plot dimensions and settings are identical in both the preview and ggsave:
- Keep
width,height, andunitsthe same - Use the exact same
ragg::agg_pngdevice in both (the preview setup above handles this) - Maintain the same
dpivalue across both preview and save
3. Verify Font Consistency (Optional but Recommended)
ragg uses system fonts directly, so make sure the font specified in your ggplot theme is available and consistent between preview and save. If using a custom font, register it first with systemfonts::register_font() to avoid fallback font differences:
# Register a custom font (replace path with your font file) systemfonts::register_font("CustomSans", regular = "~/fonts/CustomSans-Regular.ttf") # Use the registered font in your plot mtcars %>% ggplot() + geom_bar(aes(cyl)) + labs(title = "Title Title Title Title Title Title Title") + theme(plot.title = element_text(size = 30, family = "CustomSans"))
Full Working Example
Here's the adjusted version of your code with all fixes applied:
# Set preview device to ragg with 600dpi options(device = function(...) ragg::agg_tk(device = "png", dpi = 600, ...)) library(tidyverse) library(ragg) # Create the plot mtcars %>% ggplot() + geom_bar(aes(cyl)) + labs(title = "Title Title Title Title Title Title Title") + theme(plot.title = element_text(size = 30)) # Save with matching parameters ggsave("test plot.png", width = 7, height = 7/2, units = "in", device = ragg::agg_png, dpi = 600)
This works because both the preview and saved plot now use the same rendering engine (ragg) and identical dpi/scale settings, eliminating the visual discrepancies you were seeing. Unlike {showtext} which adjusts font pixel sizes manually, ragg relies on the device's dpi for consistent vector-based font rendering—so aligning the dpi across preview and save is critical.
内容的提问来源于stack exchange,提问作者Philli Phresh

