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

Docker运行R脚本时ggplot生成文本异常极小问题求助

问题:Docker环境下ggplot图表文本异常缩小且字体加载异常

我通过Drone运行Dockerfile执行R脚本,脚本借助自定义包在Google Slides生成ggplot图表。本地运行时一切正常,图表使用自定义字体和包内的ggtheme;但Docker环境下,字体被识别为Libre Franklin,调整ggtheme、手动设置文本大小及修改DPI均无效,图表文本异常极小。

Dockerfile代码:

# DOCKER + SYSTEM SET UP #######################################################

FROM rocker/tidyverse:latest

# Install system dependencies (if any) for the R package
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev \
    libssl-dev \
    libgit2-dev \
    git \
    jq \
    curl \
    wget \
    fontconfig

# Install Xvfb and X11
RUN apt-get update && apt-get install -y \
    xvfb \
    x11-xserver-utils

# Start Xvfb in the background
CMD ["Xvfb", ":99", "-ac"]

# FONT INSTALLATION CODE #######################################################

# Download the Libre Franklin font (all weights) from Google Fonts
RUN wget https://fonts.google.com/download?family=Libre%20Franklin -O libre_franklin.zip

# Unzip and install the font
RUN unzip libre_franklin.zip -d /usr/share/fonts/ && fc-cache -f -v

# Clean up
RUN rm libre_franklin.zip

# R PACKAGE INSTALLATION CODE ##################################################

# Install CRAN packages
RUN R -e "install.packages(c('devtools', 'googledrive', 'googleCloudStorageR', 'googleAuthR', 'jsonlite', 'bigrquery', 'showtext'))"

# Argument for the GitHub token
ARG GITHUB_TOKEN

# Use the GitHub token to install from a private repository
RUN git config --global url."https://$GITHUB_TOKEN:@github.com/".insteadOf "https://github.com/" && \
    R -e "devtools::install_github('nytimes/nytslider')"

# Install version-specific packages; we must do this after nytslider, since it installs them by default
RUN R -e "devtools::install_version('officer', version = '0.4.1')"
RUN R -e "devtools::install_version('flextable', version = '0.6.2')"

# Clean up GitHub token so it's not present in the image
RUN git config --global --unset url."https://$GITHUB_TOKEN:@github.com/".insteadOf

# COPY SCRIPT CODE #############################################################

COPY test_script.R /home/test_script.R
COPY mm_template.pptx /home/mm_template.pptx
COPY entrypoint.sh /entrypoint.sh

# RUN SCRIPT CODE #############################################################

# Make the entrypoint script executable
RUN chmod +x /entrypoint.sh

# Set the entrypoint script as the default command
ENTRYPOINT ["/entrypoint.sh"]
解决方案

1. 修复Xvfb启动逻辑,确保图形环境正常初始化

Docker中同时设置CMD和ENTRYPOINT时,CMD内容会作为ENTRYPOINT的参数,导致Xvfb未正确启动。修改方式:

  • 删除Dockerfile中单独的CMD ["Xvfb", ":99", "-ac"]
  • 在entrypoint.sh中添加Xvfb启动命令,确保图形上下文加载:
#!/bin/bash
# 启动Xvfb并设置DISPLAY环境变量
Xvfb :99 -ac &
export DISPLAY=:99

# 执行R脚本
Rscript /home/test_script.R

2. 确保自定义字体正确加载并被ggplot识别

  • 若使用的是自定义字体(非Libre Franklin),需在Dockerfile中安装该字体,不能只安装Libre Franklin
  • 在R脚本中显式初始化showtext,指定使用的字体:
library(showtext)
showtext_auto()
# 替换为你的自定义字体路径和名称
font_add(family = "CustomFont", regular = "/usr/share/fonts/CustomFont-Regular.ttf")

# 在ggplot中强制指定字体
ggplot(...) +
  theme(text = element_text(family = "CustomFont", size = 12))

3. 验证字体安装状态

在Dockerfile中添加验证步骤,确认字体已正确注册:

# 验证Libre Franklin是否安装成功
RUN fc-list | grep "Libre Franklin"
# 若有自定义字体,添加对应验证
RUN fc-list | grep "CustomFont"

4. 调整ggplot图形设备参数

避免无头环境下的DPI或缩放问题,生成图表时显式指定参数:

# 使用ggsave时指定高DPI和尺寸
ggsave("plot.png", plot = my_plot, dpi = 300, device = "png", width = 8, height = 6)
# 或者设置全局主题参数
theme_set(theme_minimal(base_size = 14, base_family = "CustomFont"))

内容的提问来源于stack exchange,提问作者Emily Erdos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 01:13:24