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

设置配色、图例和主题

本页目录

让颜色表达明确含义:先决定类别或连续数值,再选择映射、标度和图例。网站强调色不决定数据配色。

本页准备

在新的 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)
  )
}

从数据角色构建样式

例子明确声明类别:区间连接填色映射 query ID,序列颜色映射序列 ID,基因填色映射链。手动标度的名称与实际取值匹配。

代码
p_styled <- ggchord(seq_data = seqs, ribbon_data = links, gene_data = genes) +
  geom_seq() +
  geom_link_ribbon(aes(ribbon_fill = qaccver), alpha = .5) +
  geom_gene() +
  geom_seq_label(size = 4) +
  coord_collinear() +
  doc_axis() +
  scale_seq_colour_manual(values = seq_palette) +
  scale_gene_fill_manual(values = gene_palette) +
  scale_ribbon_fill_manual(values = seq_palette) +
  guides(gene_fill = guide_ggchord_legend(nrow = 1, theme = doc_legend_theme)) +
  doc_theme
theme_variants <- list(minimal = theme_ggchord_minimal(), dark = theme_ggchord_dark(), plasmid = theme_ggchord_plasmid())
p_styled
显式设置序列、链方向和查询类别的颜色标度
显式设置序列、链方向和查询类别的颜色标度 · 点击放大查看
角色 映射数据 常用标度家族
seq_colour, seq_ring 序列/类别 序列颜色或 ring 手动标度
ribbon_fill, ribbon_alpha, ribbon_colour, ribbon_linetype 比对元数据或统计 手动、渐变/分段/identity 和透明度标度
feature_fill, feature_shape, gene_fill, region_fill 注释类别或链 相应角色的手动/预设标度
link_colour, link_alpha, link_linewidth, link_linetype 点连接元数据 连续或手动 link标度
track_value, track_fill, track_colour 命名轨道的数值/类别 命名数值域与颜色/填色标度

固定样式和用户标度优先

常量样式写在 aes() 外,变化样式写在映射中并配合合适的标度。 显式几何层固定值优先,省略该固定参数才保持映射。用户标度优先于自动默认。 同类多层独立保留数据、映射和参数。按 accver 命名的几何向量,不表示标度 也接受逐序列列表。

组织可读图例

guide_ggchord_legend() 控制离散角色图例,guide_ggchord_colourbar() 控制连续颜色 图例。colour/color 拼写别名保留,本文优先用 colour。图层 show.legend 决定显示,guides() 选择图例,theme(legend.position = ...) 决定位置。 过宽图例可分行或增加导出宽度,不需要删减生物学数据来迁就。

为场景选择主题

例子在 theme_variants 中保存 minimal、dark 和 plasmid 备选,按场景添加到相应图中。 上图使用 theme_ggchord_publication(),plasmid 预设用于圆图流程。 主题控制非数据外观,不选择坐标系,也不替代数据驱动的填色标度。 文字外观用 theme(text = element_text(...));已删除的 base_size 式快捷参数见 迁移指南。

完成后检查

同一 query 的连接颜色一致;连续相似度应使用连续标度。固定颜色放在 aes() 外,图例必须能说明映射。

继续:调整图形 →