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

R语言绘制NMDS图报错求助:连续值与离散刻度不匹配及颜色值不足

R语言NMDS图生成报错修复方案

问题背景

使用R语言vegan和ggplot2绘制微生物丰度数据的NMDS图时,先后遇到两个报错:

  1. 首次报错:Error: Continuous value supplied to discrete scale
  2. 修改后报错:Error in f():! Insufficient values in manual scale. 4 needed but only 2 provided.

数据示例

"Level_1"   "Day"   "Yield" "Acidobacteria" "candidate division NC10"   "candidate division WOR-3"  "Candidatus Aminicenantes"  "Candidatus Eisenbacteria"  "Candidatus Rokubacteria"   "Calditrichaeota"   "Elusimicrobia" "Bacteroidetes" "Ignavibacteriae"   "candidate division Zixibacteria"   "Candidatus Latescibacteria"    "Gemmatimonadetes"  "Nitrospirae"   "Proteobacteria"    "Candidatus Abyssubacteria" "Candidatus Omnitrophica"   "Lentisphaerae" "Planctomycetes"    "Verrucomicrobia"   "Spirochaetes"  "Actinobacteria"    "Armatimonadetes"   "Chloroflexi"   "Cyanobacteria" "Deinococcus-Thermus"   "Firmicutes"
"R11P4_BS_diamond"  0   "Low"   22225.703   574.2619    0   140.88957   2322.9353   0   1029.0415   19.166956   3195.5298   256.63806   0.995686    0   1352.266    3617.2026   29759.188   0   28.37705    102.057816  4175.409    3554.35 1059.6588   5686.363    666.98517   35559.43    4108.5737   6.845341    2762.282
"R13P1_BS_diamond"  60  "High"  34864.668   296.10495   0   96.91555    970.3203    0   1011.2066   21.433247   3125.6428   382.53687   0.5824252   0   740.0294    8325.418    26701.049   0   43.33243    95.63422    2905.2532   5159.7046   1287.509    5290.517    720.45996   36515.844   3476.9617   7.105587    3380.0461
"R13P3_BS_diamond"  60  "High"  30511.857   201.64407   1252.1503   160.40663   1576.6329   0   0   20.618717   3181.9224   527.6994    0.17473489  0   618.5615    5305.126    27084.434   0   72.86445    81.251724   3752.0823   4092.466    2143.2983   6419.4106   869.3061    35032.426   3004.916    5.5915165   2518.454
"R13P6_BS_diamond"  60  "Low"   23491.105   201.67783   0   115.17029   2832.317    0   0   20.043161   2692.534    1185.2467   0   0   2077.0115   1735.1355   38256.37    0   1011.81616  99.385  4316.4453   4472.325    917.51984   9140.408    1159.6995   21273.375   3704.8694   7.061839    2208.4863
"R14P1_BS_diamond"  90  "High"  21952.639   196.00256   0   37.935978   1538.1635   0   0   0   3867.7136   1045.1714   0   0   924.8651    1710.6316   32768.605   0   0   0   3437.2456   2696.6157   1705.3628   14147.135   1296.3215   33106.695   3081.4202   11.24029    2023.2522
"R14P3_BS_diamond"  90  "High"  23845.86    189.5785    0   121.52756   1219.4237   0   0   19.843521   3474.5222   285.2086    0.3363309   0   1093.2996   4346.6284   32942.938   0   37.66906    97.19962    3969.1528   4942.8306   761.90155   9911.335    933.76666   34448.355   3229.0007   7.6235  2885.943

原始代码

library(vegan)
pc = read.csv("/media/combio7/8TB_Backup/sumitra_130122/all_season_megan_metadata/NDMS/transposed-bs-allseason-230922-taxonomy-phylum.csv", header= TRUE,sep ='	')

#make community matrix - extract columns with abundance information
com = pc[,4:ncol(pc)]

#turn abundance data frame into a matrix
m_com = as.matrix(com)

set.seed(123)
nmds = metaMDS(m_com, distance = "bray")
nmds
plot(nmds)

#extract NMDS scores (x and y coordinates)
data.scores = as.data.frame(scores(nmds)$sites)

data.scores$Sample = pc$Sample
data.scores$Time = pc$Day
data.scores$Type = pc$Yield

head(data.scores)

library(ggplot2)

xx = ggplot(data.scores, aes(x = NMDS1, y = NMDS2)) + 
    geom_point(size = 4, aes( shape = Type, colour = Time))+ 
    theme(axis.text.y = element_text(colour = "black", size = 12, face = "bold"), 
    axis.text.x = element_text(colour = "black", face = "bold", size = 12), 
    legend.text = element_text(size = 12, face ="bold", colour ="black"), 
    legend.position = "right", axis.title.y = element_text(face = "bold", size = 14), 
    axis.title.x = element_text(face = "bold", size = 14, colour = "black"), 
    legend.title = element_text(size = 14, colour = "black", face = "bold"), 
    panel.background = element_blank(), panel.border = element_rect(colour = "black", fill = NA, size = 1.2),
    legend.key=element_blank()) + 
    labs(x = "NMDS1", colour = "Type", y = "NMDS2", shape = "Time")  + 
    scale_colour_manual(values = c("#009E73", "#E69F00"))

错误原因分析

  1. 首次错误:Time(对应Day列)是数值型变量,用colour映射时,ggplot2默认将其视为连续变量,但后续用scale_colour_manual指定了离散颜色,两者冲突导致报错。
  2. 修改后错误:
    • 错误地在labs中写了colour = as.factor("Time"),这会创建一个单因子值,导致图例识别异常;
    • Time转成因子后有3个水平(0、60、90),但scale_colour_manual只提供了2个颜色值,数量不匹配。

修复后的代码

library(vegan)
library(ggplot2)

# 读取数据
pc = read.csv("/media/combio7/8TB_Backup/sumitra_130122/all_season_megan_metadata/NDMS/transposed-bs-allseason-230922-taxonomy-phylum.csv", 
              header= TRUE, sep ='	')

# 提取物种丰度矩阵
com = pc[,4:ncol(pc)]
m_com = as.matrix(com)

# 运行NMDS分析
set.seed(123)
nmds = metaMDS(m_com, distance = "bray")

# 提取位点得分并合并分组信息
data.scores = as.data.frame(scores(nmds)$sites)
data.scores$Sample = pc$Level_1  # 原代码中pc$Sample不存在,替换为Level_1列
data.scores$Time = as.factor(pc$Day)  # 提前将Time转为因子
data.scores$Type = as.factor(pc$Yield)  # 提前将Type转为因子

# 绘制NMDS图
xx = ggplot(data.scores, aes(x = NMDS1, y = NMDS2)) + 
    geom_point(size = 4, aes(shape = Type, colour = Time)) +
    # 主题设置
    theme(axis.text.y = element_text(colour = "black", size = 12, face = "bold"), 
          axis.text.x = element_text(colour = "black", face = "bold", size = 12), 
          legend.text = element_text(size = 12, face ="bold", colour ="black"), 
          legend.position = "right", 
          axis.title.y = element_text(face = "bold", size = 14), 
          axis.title.x = element_text(face = "bold", size = 14, colour = "black"), 
          legend.title = element_text(size = 14, colour = "black", face = "bold"), 
          panel.background = element_blank(), 
          panel.border = element_rect(colour = "black", fill = NA, size = 1.2),
          legend.key=element_blank()) + 
    # 图例标签设置
    labs(x = "NMDS1", colour = "Time", y = "NMDS2", shape = "Yield") +
    # 为Time的3个水平提供对应颜色
    scale_colour_manual(values = c("#009E73", "#E69F00", "#D55E00")) +
    # 为Type的2个水平设置形状
    scale_shape_manual(values = c(16, 17))

# 查看图形
print(xx)

关键修复点

  • 提前将Time和Type转为因子,避免绘图时类型冲突;
  • 修正labs中的标签映射,确保colour对应Time,shape对应Yield;
  • 为Time的3个水平(0、60、90)提供3个颜色值,保证数量匹配;
  • 补充scale_shape_manual明确指定形状,让图形更规范;
  • 原代码中pc$Sample不存在,替换为数据中的Level_1列作为样本标识。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 09:42:05