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

如何用ggplot2将列表元素名设为散点图标题并按转录本名保存

解决方案

修改绘图代码(添加转录本名作为标题)

使用purrr::imap()替代map(),它能同时获取子列表的数据和对应的名称(也就是你的转录本名),再通过labs()设置图标题:

library(purrr)
library(ggplot2)

plots <-
  big_list2 |>
  imap(\(data, transcript_name) {
    ggplot(data, aes(x = uORF.counts, y = mORF.counts, label = sample)) +  
      geom_point() + 
      geom_smooth(method = lm) + 
      geom_text(hjust = 0, vjust = 0) +
      labs(
        x = "Counts in uORF (TPM)", 
        y = "Counts in mORF (TPM)",
        title = transcript_name  # 用子列表名称作为图表标题
      )
  })

修改保存代码(文件名包含转录本名)

同样用imap()遍历生成的plots列表,直接用转录本名命名文件:

imap(plots, \(plot, transcript_name) {
  ggsave(
    filename = paste0(transcript_name, "_uORF_mORF_plot.png"),  # 文件名包含转录本名
    plot = plot,
    width = 8,  # 可按需调整图片尺寸
    height = 6
  )
})

关键改动说明

  • imap()的作用:它是map()的变体,会把列表的元素值和元素名称分别传递给函数的两个参数(上述代码里的data和transcript_name),解决了你无法获取子列表名称的核心问题。
  • 移除无效参数:ggplot2中aes()里的main参数不会生效,必须通过labs(title=...)或ggtitle()设置图标题。
  • 文件名优化:用转录本名替代数字序号作为文件名核心,方便后续识别和管理图片。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:01:13