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

基于ggplot2制作会议胸牌的尺寸与西里尔文偏移问题求助

Hey there! Let's tackle your two badge issues one by one—first the sizing mismatch, then the Cyrillic text offset. Both are fixable with targeted tweaks to your code and setup.

1. Fixing the Badge Size (50×85mm instead of 64.5×89.1mm)

The core problem here is that your ggplot coordinates, facet layout, and PDF output dimensions weren't aligned to your physical target size. Let's fix that step by step:

Why it's happening:

  • Your original xlim(0,5) and ylim(0,8.5) used arbitrary units, not tied to actual millimeters/inches.
  • ggplus::facet_multiple doesn't enforce strict per-facet sizing by default, especially when paired with a misconfigured PDF page.
  • A4 paper is 210×297mm, so we can fit 3 columns × 3 rows of 50×85mm badges (3×50=150mm <210mm; 3×85=255mm <297mm).

Step-by-step solution:

First, convert your target dimensions to inches (R uses inches for PDF sizing):

  • 50mm = ~1.9685 inches (width)
  • 85mm = ~3.3465 inches (height)

Adjust your ggplot template and export workflow to match these exact sizes:

library(tidyverse)
library(stringr)
library(grid)
library(magick)
library(ggforce) # More reliable for pagination than ggplus

# Load English name data
names <- tibble(name = c("Jared Harris", "Stellan Skarsgеrd", "Paul Ritter", "Jessie Buckley", "Adam Nagaitis", "Con O'Neill", "Adrian Rawlins", "Sam Troughton", "Robert Emms", "Emily Watson", "David Dencik", "Mark Lewis Jones", "Alan Williams", "Alex Ferns", "Ralph Ineson", "Barry Keoghan","Fares Fares", "Michael McElhatton"))

# Download logos
logo1 <- image_read("https://www.stickpng.com/assets/images/5cb58699cba3dd2a29200cb6.png")
logo2 <- image_read("https://www.stickpng.com/assets/images/5cb58635cba3dd2a29200caa.png")

# Build badge template with exact physical dimensions
ggplot(data = names) + 
  # Center name text within the badge bounds
  geom_text(aes(label = str_wrap(str_to_upper(name), width = 15)), 
            fontface = 2, x = 0.984, y = 0.5, size = 5) +
  # Add centered conference info
  annotate("text", x = 0.984, y = 2.7, 
           label = "3-6 september, 2019\nCity,\nName of the Conference", size = 3) +
  # Set x/y limits to match 50×85mm (converted to inches)
  xlim(c(0, 1.9685)) + ylim(c(0, 3.3465)) +
  # Clean theme with borders and spacing
  theme_void() %+replace% 
  theme(panel.border = element_rect(fill = NA, colour = "grey60"),
        strip.text = element_blank(),
        panel.spacing = unit(c(10, 10), "mm")) + # Add gaps between badges
  # Scale logos to fit the badge
  annotation_custom(rasterGrob(logo1, interpolate=TRUE), 
                    xmin=1.18, xmax=1.9685, ymin=2.5, ymax=3.3465) +
  annotation_custom(rasterGrob(logo2, interpolate=TRUE), 
                    xmin=0, xmax=0.788, ymin=2.5, ymax=3.3465) -> p

# Export to A4 PDF with pagination
total_pages <- ceiling(nrow(names)/9) # 9 badges per page
pdf("badges/badges.pdf", paper = "a4", width = 210/25.4, height = 297/25.4)
for(page in 1:total_pages){
  print(p + facet_wrap_paginate(~name, ncol=3, nrow=3, page=page))
}
dev.off()

Key improvements:

  • Replaced arbitrary coordinates with exact inch values matching your 50×85mm target.
  • Switched to ggforce::facet_wrap_paginate for precise control over per-facet sizing and page breaks.
  • Adjusted logo positions and panel spacing to fit the new badge dimensions.

2. Fixing Cyrillic Text Offset

The text shift occurs because R isn't using a font that properly supports Cyrillic characters, leading to misaligned rendering. Here's how to fix it:

Why it's happening:

  • Windows default fonts may not handle Cyrillic correctly, and ggplot wasn't explicitly told to use a compatible font.
  • While your locale is set to Russian_Russia.1251 (correct), font matching needs to be explicit.

Step-by-step solution:

  1. Use a Cyrillic-friendly font: On Windows, "Arial Unicode MS", "Times New Roman", or "Segoe UI" all support Cyrillic characters.
  2. Update your ggplot to use this font:
# Load Cyrillic name data
names <- structure(list(name = c( "Айбулатов Денис Николаевич", "Александрин М.Ю.", "Александровский Алексей Юрьевич", "Андреев П.В.", "Антоненко М.В.", "Атутова Жанна Владимировна", "Бабич Дмитрий Борисович", "Баженова Ольга Иннокентьевна", "Бажин К.И.", "Барабанов Анатолий Тимофеевич")), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame"))

# Build Cyrillic badge template
ggplot(data = names) + 
  geom_text(aes(label = str_wrap(str_to_upper(name), width = 12)), # Narrower wrap for longer Cyrillic names
            fontface = 2, x = 0.984, y = 0.5, size = 4.5,
            family = "Arial Unicode MS") + # Explicit Cyrillic font
  annotate("text", x = 0.984, y = 2.7, 
           label = "3-6 сентября, 2019\nГород,\nНазвание Конференции", 
           size = 3, family = "Arial Unicode MS") + # Match font for annotations
  xlim(c(0, 1.9685)) + ylim(c(0, 3.3465)) +
  theme_void() %+replace% 
  theme(panel.border = element_rect(fill = NA, colour = "grey60"),
        strip.text = element_blank(),
        panel.spacing = unit(c(10, 10), "mm")) +
  annotation_custom(rasterGrob(logo1, interpolate=TRUE), 
                    xmin=1.18, xmax=1.9685, ymin=2.5, ymax=3.3465) +
  annotation_custom(rasterGrob(logo2, interpolate=TRUE), 
                    xmin=0, xmax=0.788, ymin=2.5, ymax=3.3465) -> p_cyrillic

# Export Cyrillic badges
pdf("badges/badges_cyrillic.pdf", paper = "a4", width = 210/25.4, height = 297/25.4)
total_pages_cyr <- ceiling(nrow(names)/9)
for(page in 1:total_pages_cyr){
  print(p_cyrillic + facet_wrap_paginate(~name, ncol=3, nrow=3, page=page))
}
dev.off()

Troubleshooting font issues:

If "Arial Unicode MS" isn't available, run extrafont::font_import() once to register all system fonts with R, then check available Cyrillic fonts with extrafont::fonts().


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:58:34