如何使用 emmip 或 emmip_ggplot 更改 x 轴上项的顺序?

问题描述 投票:0回答:1

我需要一些帮助。我试图在使用

emmip
函数时更改 x 轴项。有问题的两个项目都已编码为因素(
Ploidy
P
)。
x
是我的数据。

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  

我运行了广义线性混合模型,并使用

emmip
可视化数据。 gammaP 是我的模型。

install.packages("emmeans")
library(emmeans)
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")

我收到情节: emmip image

我需要调换图中三倍体和四倍体的顺序。我尝试用这段代码来做到这一点:

x$Ploidy <- factor(x$Ploidy, levels=c("Diploid", "Triploid", "Tetraploid"))
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")

正如您所看到的,轴标签发生了变化,但绘图上的实际点没有变化。

bad emmip plot

为什么只有轴发生变化?我不应该重新编码我的数据吗?

我用

emmip_ggplot
再次尝试了这一点。由于
emmip_ggplot
使用与
ggplot
相同的语法,但轴项和点都发生了变化。

我将数据重新编码为一个角色,然后制作了一个绘图来使用

emmip_ggplot

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 轴没有再次改变,我担心我只会再次改变轴项...... more bad plots

谢谢大家!

如果您想查看我的数据或模型的快照,请告诉我!

r ggplot2 emmeans
1个回答
0
投票

x 轴上的类别顺序通常由

factor
的类别级别顺序决定。由于您在转换为
factor
时未设置顺序,因此您将获得默认顺序,即类别按字母顺序排序。

因此,要解决您的问题,请在转换为

factor
时设置所需的顺序。

改编

?emmeans::emmip
中的默认示例:

library(emmeans)
library(ggplot2)

dat <- auto.noise

# Set the order of the levels 
dat$size <- factor(dat$size, c("L", "S", "M"))
noise.lm <- lm(noise ~ size * type, data = dat)

emmip(noise.lm, type ~ size)

或者作为第二个选项,您可以使用

limits=
scale_x_discrete
参数设置类别在 x 轴上的顺序:


# Set the order using scale_x_discrete
emmip(noise.lm, type ~ size) +
  scale_x_discrete(limits = c("M", "S", "L"))

© www.soinside.com 2019 - 2024. All rights reserved.