跳到正文
0.16.6 文档
全站目录 · 按任务查找

预览和保存图形

本页目录

按真实导出尺寸检查图形,再用标准 ggsave() 保存,并了解布局表与诊断可以证明什么。

本页准备

在新的 R 会话中先运行下方代码,再按页面顺序继续。配色和排版是可选的显示设置,输入只选择需要的列。

准备代码 · 在本页运行一次
代码
library(ggchord)
library(dplyr)
data(seq_data_example)
data(ribbon_data_example)
data(gene_data_example)
seqs <- seq_data_example |>
  select(accver, length)
links <- ribbon_data_example |>
  select(qaccver, saccver, qstart, qend, sstart, send)
# The fixture preserves biological strands separately from its old display strands.
genes <- gene_data_example |>
  transmute(accver, start, end, strand = source_strand)

# Optional appearance choices; none of these columns or scales is required input.
library(ggchord)
seq_palette <- setNames(
  c("#E64B35", "#233E4A", "#269B82", "#D59B17"),
  c("MT108731.1", "MT118296.1", "OQ646790.1", "OR222515.1")
)
gene_palette <- c("+" = "#CE1536", "-" = "#346B9D", "." = "#8291A6")

library(ggchord)
doc_background <- "#FFFCF5"
identity_palette <- c("#303B71", "#36537D", "#269B82", "#A5D52C", "#ECEB31")
identity_values <- c(0, 0.5, 0.8, 0.9, 1)
identity_breaks <- c(0, 50, 80, 90, 100)
doc_legend_theme <- theme(
  legend.title = element_text(size = 13, face = "bold"),
  legend.text = element_text(size = 11),
  legend.key.width = unit(9, "mm"), legend.key.height = unit(5, "mm"),
  legend.spacing.x = unit(9, "mm"), legend.spacing.y = unit(7, "mm"),
  legend.margin = margin(l = 4, r = 4, unit = "mm")
)
sequence_guide <- guide_ggchord_legend(nrow = 1, theme = doc_legend_theme)
strand_guide <- guide_ggchord_legend(
  nrow = 1, override.aes = list(strand = c("+", "-")), theme = doc_legend_theme
)
identity_guide <- guide_ggchord_colourbar(
  position = "left", direction = "vertical",
  theme = theme(
    legend.key.height = unit(75, "mm"), legend.key.width = unit(5, "mm"),
    legend.title = element_text(size = 13, face = "bold"),
    legend.text = element_text(size = 11), legend.margin = margin(r = 7, unit = "mm")
  )
)
doc_theme <- theme_ggchord_publication(
  axis.text = element_text(size = 11),
  axis.text.check.overlap = TRUE
) +
  theme(
    legend.position = "bottom",
    legend.title = element_text(size = 13, face = "bold"),
    legend.text = element_text(size = 11),
    legend.key.width = unit(9, "mm"), legend.key.height = unit(5, "mm"),
    legend.spacing.x = unit(9, "mm"), legend.spacing.y = unit(7, "mm"),
    legend.box.spacing = unit(6, "mm"),
    plot.background = element_rect(fill = doc_background, colour = NA),
    panel.background = element_blank(),
    plot.title = element_text(face = "bold", size = 18),
    plot.subtitle = element_text(size = 10, colour = "#5C625A"),
    plot.margin = margin(14, 18, 14, 18)
  )
doc_axis <- function(coordinates = "relative") {
  scale_seq_position_continuous(
    coordinates = coordinates, n.breaks = 3, minor_breaks = NULL,
    labels = function(x) format(x, scientific = FALSE, big.mark = ",", trim = TRUE)
  )
}

anchors <- genes |>
  group_by(accver) |>
  slice_max(end - start, n = 1, with_ties = FALSE) |>
  ungroup() |>
  select(accver, start, end)
p_collinear <- ggchord(seq_data = seqs, ribbon_data = links, gene_data = genes) +
  geom_link_ribbon(aes(ribbon_fill = qaccver), alpha = .7) +
  scale_ribbon_fill_manual(values = seq_palette, guide = "none") +
  geom_seq() +
  geom_gene(position = position_strand(.1)) +
  geom_seq_label(size = 4) +
  coord_collinear(anchor = anchors, align = "center", orientation = "auto") +
  doc_axis() +
  scale_seq_colour_manual(values = seq_palette) +
  scale_gene_fill_manual(values = gene_palette) +
  doc_theme
p_collinear

按目标尺寸保存

先在三种模式中创建 p_collinear:

代码
ggsave("comparison.png", p_collinear, width = 8, height = 5, units = "in", dpi = 144)

RStudio Plots 使用当前面板尺寸,标签与图例可能随面板调整而重新排列。 最终 ggsave() 前,按相同显式尺寸预览:

代码
view_ggchord(p_collinear, width = 8, height = 5, units = "in")
diagnosis <- diagnose_ggchord(p_collinear, width = 8, height = 5, units = "in")

view_ggchord() 返回临时图片路径,不保存最终导出文件。 网站、README 和 vignette 共用标准 ggsave() 生成的 PNG,尺寸见图集, 不截取临时调整后的交互面板。最终矢量输出应显式选择目标 PDF/SVG 设备;字体可用性 和嵌入仍取决于设备。

按字段名消费几何

代码
geometry <- export_ggchord_layout(p_collinear, include = "seq")
rows <- geometry$seq |>
  distinct(layer_id, component, source_row)

export_ggchord_layout() 是推荐的分层表格接口;get_chord_layout() 保留完整的 已记录布局,两者都未弃用。选择字段名,不依赖固定列位置。 source_row 指已解析图层输入行,ID 是构建期资源,不是永久生物学标识。 详见完整布局结果说明。

理解诊断证据

diagnose_ggchord() 区分面板已测量与几何收敛。面板已测量不表示文字无碰撞、字体 已嵌入或跨应用表现已认证。应检查警告、文字/图例测量及放置原因。 optimize_ggchord_layout() 按文档执行主动选择的调整,应保留副本并查看结果。 预览和诊断使用独立渲染副本。直接改动共享 ggproto 图层环境仍遵循普通 ggplot2 引用语义,不会自动隔离为一次独立编辑。

完整布局、表格与主动选择的排序

代码
full_layout <- get_chord_layout(p_collinear)
layer_tables <- export_ggchord_layout(p_collinear, include = c("seq", "gene"), original_data = TRUE)
layout_choice <- optimize_ggchord_layout(seqs, links, optimize = c("order", "orientation"), weight = "count")

先在三种模式创建 p_collinear。 full_layout 保留完整的已记录 getter 结果,layer_tables 选择序列/基因表与已解析 原始数据,不是原始文件档案。layer ID 和源行角色区分独立同类图层。 空组件可没有固定列原型,应按字段名读取。

layout_choice 以计数权重选择顺序/方向,因为本例的最小绘图表有意未保留比对长度统计列。 返回的选择应显式用于序列设置,不修改原图,也不确立生物学方向。 采用前应保存该选择并对照结果。文档精确尺寸见资产清单。

完成后检查

预览与保存使用相同宽高。检查文字、图例与警告;一次测量成功不代表跨设备字体和发布质量已认证。

继续:常见问题 →