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

如何在ggord(基于ggplot)中修改绘图图例标签?

修改ggord绘图的图例标签

我正在尝试修改ggord(非标准ggplot2)绘图中的图例标签,需要将原标签"blamm"改为"Airborne","sccoos"改为"seawater"。当前生成绘图的代码如下:

library(ggord)
sample_types <- c("Airborne", "Seawater")
ggord(
  simpleRDA3,
  taxa_ord_meta$sample_type,
  xlims = c(-0.5, 1.1),
  ylims = c(-0.52, 1.1),
  repel = TRUE,
  grp_title = "Sample Type")+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())+
  ggtitle("Ordination by Distance Based Redundancy Analysis", "Community defining bacteria, within the Sea Surface-Atmospheric Boundary Layer")+
  labs(caption = str_wrap("Refer to Table # for details of specfic taxa. The 12 taxa represented are the top 6 most sifnficantly different, and the top 6 highest relatice abundance."))+
  theme_bw()

解决方案

方法一:提前修改分组变量的因子水平

ggord会直接沿用传入分组变量的标签,因此可以先替换taxa_ord_meta$sample_type的因子水平:

# 重新定义因子水平与对应标签
taxa_ord_meta$sample_type <- factor(taxa_ord_meta$sample_type,
                                    levels = c("blamm", "sccoos"),
                                    labels = c("Airborne", "seawater"))

# 运行修改后的绘图代码
library(ggord)
ggord(
  simpleRDA3,
  taxa_ord_meta$sample_type,
  xlims = c(-0.5, 1.1),
  ylims = c(-0.52, 1.1),
  repel = TRUE,
  grp_title = "Sample Type")+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())+
  ggtitle("基于距离冗余分析的排序图", "海-气边界层中定义群落的细菌")+
  labs(caption = str_wrap("具体类群详情请参考表#。图中展示的12个类群为差异最显著的前6个和相对丰度最高的前6个。"))+
  theme_bw()

方法二:在ggplot层中覆盖图例标签

ggord返回的是标准ggplot对象,可直接通过scale_color_discrete或scale_fill_discrete修改标签(根据ggord的配色逻辑选择,分组点通常用color,填充类元素用fill):

library(ggord)
ggord(
  simpleRDA3,
  taxa_ord_meta$sample_type,
  xlims = c(-0.5, 1.1),
  ylims = c(-0.52, 1.1),
  repel = TRUE,
  grp_title = "Sample Type")+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())+
  ggtitle("基于距离冗余分析的排序图", "海-气边界层中定义群落的细菌")+
  labs(caption = str_wrap("具体类群详情请参考表#。图中展示的12个类群为差异最显著的前6个和相对丰度最高的前6个。"))+
  # 修改点颜色的图例标签
  scale_color_discrete(labels = c("blamm" = "Airborne", "sccoos" = "seawater")) +
  # 若涉及填充色,取消注释下面的语句
  # scale_fill_discrete(labels = c("blamm" = "Airborne", "sccoos" = "seawater")) +
  theme_bw()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 21:09:24