You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何使用ggsave保存ggplot图形时文本会变大?如何使RStudio内联预览与保存结果一致?

Fix Inline Preview vs GGSaved Plot Discrepancy with ragg in 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 in ggsave)
  • 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, and units the same
  • Use the exact same ragg::agg_png device in both (the preview setup above handles this)
  • Maintain the same dpi value across both preview and save

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:17:45