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

如何使用emmip或emmip_ggplot调整x轴分类项的顺序?

调整emmip绘图中x轴分类项顺序的问题

背景与已执行步骤

  • 将数据集中的Ploidy和P转换为因子类型:
x$Ploidy <- as.factor(x$Ploidy)
x$P <- as.factor(x$P)
summary(x)
  • 数据集摘要输出:
Test tube number  Sample ID           Lineage                 Ploidy      P         absorb 1       
 Min.   :  1.00   Length:70          Length:70          Diploid   :22   High:34   Min.   :0.04300  
 1st Qu.: 46.25   Class :character   Class :character   Triploid  :21   Low :36   1st Qu.:0.05425  
 Median : 80.50   Mode  :character   Mode  :character   Tetraploid:27             Median :0.05700  
 Mean   : 79.04                                                                   Mean   :0.06189  
 3rd Qu.:109.75                                                                   3rd Qu.:0.06600  
 Max.   :149.00                                                                   Max.   :0.09800  
    absorb 2         absorb avg           +/-          absorb x 1.73        dilution          mg P          
 Min.   :0.04600   Min.   :0.04450   Min.   :0.01270   Min.   :0.07698   Min.   :0.007   Min.   :8.893e-05  
 1st Qu.:0.05500   1st Qu.:0.05613   1st Qu.:0.05577   1st Qu.:0.09710   1st Qu.:0.007   1st Qu.:3.904e-04  
 Median :0.06000   Median :0.05900   Median :0.06642   Median :0.10207   Median :0.007   Median :4.649e-04  
 Mean   :0.06147   Mean   :0.06168   Mean   :0.07634   Mean   :0.10670   Mean   :0.007   Mean   :5.344e-04  
 3rd Qu.:0.06600   3rd Qu.:0.06650   3rd Qu.:0.09420   3rd Qu.:0.11504   3rd Qu.:0.007   3rd Qu.:6.594e-04  
 Max.   :0.08900   Max.   :0.08950   Max.   :0.17940   Max.   :0.15484   Max.   :0.007   Max.   :1.256e-03  
   mg sample         USE mg sample snails corrected   P_content       
 Min.   :0.0000040   Min.   :0.0040                 Min.   :0.000186  
 1st Qu.:0.0002365   1st Qu.:0.2365                 1st Qu.:0.010775  
 Median :0.0004020   Median :0.4020                 Median :0.019333  
 Mean   :0.0004277   Mean   :0.4277                 Mean   :0.024214  
 3rd Qu.:0.0006230   3rd Qu.:0.6230                 3rd Qu.:0.030923  
 Max.   :0.0015900   Max.   :1.5900                 Max.   :0.125460  
  • 运行广义线性混合模型gammaP后,使用emmip可视化数据:
install.packages("emmeans")
library(emmeans)
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")

问题与无效尝试

需要调换图中Triploid和Tetraploid的顺序,以下操作均未达到预期效果:

  1. 重新指定Ploidy因子水平后绘图,仅x轴标签变化,对应数据点顺序未改变:
x$Ploidy <- factor(x$Ploidy, levels=c("Diploid", "Triploid", "Tetraploid"))
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")
  1. 使用emmip_ggplot调整x轴顺序,结果x轴分类项顺序仍未改变:
x$Ploidy <- as.character(x$Ploidy)
emmip(gammaP, P ~ Ploidy)
badplot <- emmip(gammaP, P ~ Ploidy, plotit = FALSE)
emmip_ggplot(badplot, P ~ Ploidy, aes(x = factor(Ploidy, level = c('Diploid', 'Triploid', 'Tetraploid'))))

求助需求

寻求能真正调整x轴分类项(连同对应数据点)顺序的方法,可提供数据集片段或模型详情。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:30:21