I need some help. I am trying to change the x axis terms while using the emmip
function. Both of the items in question have been coded as factors (Ploidy
and P
). x
is my data.
x$Ploidy <- as.factor(x$Ploidy)
x$P <- as.factor(x$P)
summary(x)
Output of summary:
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
I ran a generalized linear mixed model and visualized the data using emmip
. gammaP is my model.
install.packages("emmeans")
library(emmeans)
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")
I need to switch the order of Triploid and Tetraploid in the graph. I attempted to do this with this code:
x$Ploidy <- factor(x$Ploidy, levels=c("Diploid", "Triploid", "Tetraploid"))
emmip(gammaP, P ~ Ploidy, xlab = "Ploidy Level")
As you can see the axis labels changed but not the actual points on the plot.
Why is only the axis changing? Should I have not recoded my data?
I attempted this again with emmip_ggplot
. As emmip_ggplot
uses the same syntax as ggplot
, but the axis terms nor the points change.
I recoded my data to a character and then made a plot to use 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'))))
The x axis did not change again and I am concerned that I will only change the axis terms again...
Thank you all!
Please let me know if you would like to see a snap of my data or the model!
The order of the categories on the x axis is in general determined by the order of the levels of the categories of a factor
. As you have not set the order when converting to a factor
you get the default order, i.e. the categories are ordered alphabetically.
Hence, to fix your issue set your desired order when converting to a factor
.
Adapting the default example from ?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)
Or as a second option you can set the order of the categories on the x axis using the limits=
argument of scale_x_discrete
:
# Set the order using scale_x_discrete
emmip(noise.lm, type ~ size) +
scale_x_discrete(limits = c("M", "S", "L"))