I have a data set:
df<- structure(list(Depth = c(6.83999999999997, 8.56, 4.64999999999998,
8.83999999999997, 6.56, 8.64999999999998, 12.21, 11.82, 5.41000000000003,
11.63, 9.41000000000003, 11.26, 8.95999999999998, 10.81, 10.68,
12.74, 14.06, 8.16000000000003, 12.31, 10.76, 10.74, 1, 9.38,
5, 4, 12, 6.70999999999998, 8.56, 14.65, 16.71, 12.56, 18.65,
20.21, 11.82, 13.41, 13.63, 13.41, 13.26, 22.96, 14.81, 20.74,
30.06, 30.16, 32.31, 32.21, 14.76, 14.74, 4.66000000000003, 10,
4, 15, 8.70999999999998, 32.65, 26.21, 29.82, 29.41, 5.63, 23.41,
29.26, 2.95999999999998, 2.81, 2.68000000000001, 2.74000000000001,
2.06, 2.16000000000003, 2.31, 4.20999999999998, 8.75999999999999,
2.74000000000001, 18.66, 3, 4, 20, 6.83999999999997, 1, 6.64999999999998,
6.20999999999998, 1.81999999999999, 1.41000000000003, 3.63, 3.41000000000003,
5.25999999999999, 2.95999999999998, 2.81, 1, 2.74000000000001,
4.06, 4.16000000000003, 4.31, 4.20999999999998, 2.75999999999999,
2.74000000000001, 1, 5, 3, 4.70999999999998, 2.56, 2.64999999999998,
10.21, 7.81999999999999), NEAR_DIST = c(18.77925552, 18.30180262,
61.36019078, 179.2770495, 10.43166516, 17.9171804, 46.20571245,
31.99340507, 10.43166516, 26.7170903, 24.47782541, 33.08965222,
27.27138524, 43.4212158, 46.0670014, 50.11661352, 47.39692573,
64.4374351, 49.66872737, 12.12884673, 15.13068812, 25.02246826,
10.46189005, 13.46373164, 16.89230952, 13.51981867, 32.50661183,
38.24201162, 38.5502434, 82.06185032, 49.57486607, 90.64395203,
83.61730031, 49.74483449, 397.2686612, 53.49338859, 68.02475678,
59.6583949, 130.7528811, 67.27058895, 111.2988217, 347.3593823,
220.5169227, 268.5649787, 194.9220113, 84.48739079, 57.1344938,
24.35529161, 54.84148996, 18.74063124, 66.63864028, 203.7119682,
829.3788162, 309.4190672, 395.4959263, 326.7671063, 35.65309711,
264.2374189, 307.025746, 23.02085763, 26.3683775, 22.93486062,
25.28307029, 15.49632807, 14.59667995, 13.36925569, 11.9476145,
152.7517309, 11.30381957, 74.36911773, 3.773174432, 6.825998674,
79.40020637, 38.8451901, 3.853365482, 34.8719427, 38.02805106,
21.06138328, 20.76016614, 37.60511548, 25.71672169, 41.9543577,
26.1675823, 26.1675823, 16.49388675, 29.12695505, 29.12695505,
25.21064884, 27.6250245, 25.21064884, 21.06138328, 18.59893184,
11.08799823, 19.92747995, 16.25210115, 18.52964249, 5.582718512,
10.11944373, 56.29794875, 36.03064946), Season2 = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L), levels = c("Winter", "Spring", "Summer", "Fall"), class = c("ordered",
"factor"))), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
and am running a gam with the data:
library(mgcv)
library(gratia)
gam<-gam(Depth~s(NEAR_DIST)+Season2,data=df)
even though the Season2
variable is ordered:
unique(df$Season2)
[1] Summer Fall Winter Spring
Levels: Winter < Spring < Summer < Fall
when I call:
draw(parametric_effects(gam))
The order of the x-axis is alphabetical.
How can I get the x-axis to match the order of my factor here? The old version of gratia used to do this. I have: version 0.8.1.34
As a workaround or fix for your issue you could set the order using the limits
argument of scale_x_discrete
:
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-42. For overview type 'help("mgcv-package")'.
library(gratia)
library(ggplot2)
packageVersion("gratia")
#> [1] '0.8.1.34'
gam <- gam(Depth ~ s(NEAR_DIST) + Season2, data = df)
draw(parametric_effects(gam)) +
ggplot2::scale_x_discrete(limits = levels(df$Season2))