0
votes

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:

  1. 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.

  2. 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!

1
Can you include a some of the ExampleData? Including sample data using the dput(head(x)) function will help others answer your question. - ravic_
There are multiple questions here. I'd split out the axis titles issues from the faceting plots/axis -- the separate questions might help the community read and answer. - ravic_
Hello Ravic, before posting my question I tried to upload a data file but I couldn't find a way to do it. Is there a way to do this that I missed? - Dodo
Regarding separating the two questions, I thought I had done this by numbering them. This is only the third time I post a question, is there a particular way to post two related questions? - Dodo
Here's a link describing how to structure a good reproducible question, including how to include sample data: stackoverflow.com/questions/5963269/… - ravic_

1 Answers

0
votes

(UPDATED: Added greek math symbols)

Let's tackle the labels, which are pretty thorny in your example. I'm using the iris dataset here as a sample, but using your axis titles.

The key is using bquote() to get the math, the dynamic variables and everything else ready.

library(tidyverse)
mu_val <- 5.1
bq_x <- bquote("Predicted (" ~ mu ~ "=" ~ .(mu_val) ~ " mol " ~ m^-2 ~ s^-1 ~ ")")
bq_y <- bquote("Observed (" ~ mu ~ "=" ~ .(mu_val) ~ "mol m" ~ m^-2 ~ s^-1 ~ ")")

iris %>%
  ggplot(aes(Sepal.Length, Petal.Length)) +
  geom_point() +
  labs(title = "test", x = bq_x, y = bq_y)

Created on 2019-11-19 by the reprex package (v0.3.0)

@ravic_ thanks to your advise I am now able to produce a faceted plot with the correct labels using the revised code below:

My updated my ggplot code:

bq_x <- bquote("Predicted (" ~mu~"mol" ~ m^-2 ~ s^-1 ~ ")")
bq_y <- bquote("Observed (" ~mu~"mol" ~ m^-2 ~ s^-1 ~ ")")

# 1a. Plots by PFTs
ModFit <- ggplot(ExampleData, aes(y = Y, x = X, color = Level)) +
  geom_point() +
  theme_bw() +
  theme_classic() + 
  facet_wrap(~ Type, nrow = 2, ncol = 3) +
  # customise facet labels
  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)) +
  labs(x = bq_x, y = bq_y) +
  geom_abline(data = smaSummary, aes(intercept = elevation, slope = slope))

The two remaining problems now are: 1. Add Y and X axis lines with ticks across all faceted plots 2. Add the corresponding single SMA reg line to each plot

Many thanks again for all your help @ravic_!