I would be very grateful for advise on how to plot Standardised Major Axis (SMA) regressions lines into a faceted ggplot. I used the following code:
Run the SMA analysis and create a data frame with the SMA reg line coefficients (intercept and slope) I want to plot
smaReg = sma(Y ~ X * Type, data = ExampleData)
summary(smaReg)
smaSummary <- data.frame(Type = 1:6,coef(smaReg))
ggplot code using geom_abline to plot SMA regressions
ModFit <- ggplot(ExampleData, aes(y = Y, x = X, color = Level)) +
geom_point() +
theme_bw() +
theme_classic() +
facet_wrap(~ Type, nrow = 2, ncol = 3) +
theme(strip.background = element_blank(), strip.text = element_text(face = 'bold', size = 12)) +
annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, color = 'black', size = 1) +
annotate("segment", x = -Inf, xend = -Inf, y = -Inf, yend = Inf, color = 'black', size = 1) +
scale_x_continuous(breaks = seq(from = 0, to = 60, by = 20)) +
scale_y_continuous(breaks = seq(from = 0, to = 120, by = 20)) +
geom_abline(data = smaSummary, aes(intercept = elevation, slope = slope)) +
labs(x = expression(paste("Predicted (",mu,"mol m"^{-2},"s"^{-1},")")), y = expression(paste("Observed (",mu,"mol m"^{-2},"s"^{-1},")"))) +
ModFit
This code has two remaining issues I need to resolve but my beginner coding skills are not good enough yet to tackle them successfully:
I used annotate() and scale_x_continuous to plot the same axes and scales across all faceted plots, however, this solution does not plot the X axis ticks and I haven't found a way to do this without something else going wrong when I make a change.
When I run this plot code, I get the error message below:
Error in wrap_dims(n, params$nrow, params$ncol) : nrow * ncol >= n is not TRUE
In trying different ways of solving this error, I noticed that if I change the labs() layer to the very simplified version shown below:
labs(x = expression(X), y = expression(Y), color = "Level") +
This change produces a faceted plot but with all the SMA regressions on each plot. I have no idea why changing the labs() layer allows the plot to be produced! I have run out of ideas (and google searches) on how to plot only the corresponding SMA reg line for each plot while also adding the detailed axis labels I need without something else going wrong.
Faceted plot with simplified labels and all SMA reg lines on each plot
Many thanks in advance for any advise on how to solve these two remaining issues!