Adding title to autoplotly

647 views Asked by At

I used autoplot to create simple lm plots by using the code below. But when I use autoplotly to make an interactive plot, the title disappears that's generated by autoplot, and even if I use ggplot2::ggtitle, the title still doesn't show up. How can I fix this? The CSV can be downloaded from here.

# Read file
df = read,csv(Mean_SWE.csv)
# Run the model
Model = lm(formula = SWE ~ Mean.Z + Intensity.mean, data = df)
# Plot
lm.plot.rsd = autoplot(Model, label.size = 3, which = 1) +
  theme_bw()
autoplotly(lm.plot.rsd) +
  ggplot2::ggtitle("Residuals vs Fitted")

enter image description here

1

There are 1 answers

0
damir On BEST ANSWER

autoplot creates ggmultiplot object (lm.plot.rsd) which is stripped of all of its titles in autoplotly in order to call plotly functions properly. The solution is to pass ggplot object to autoplotly. So, take the ggplot that you need out of ggmultiplot, like this lm.plot.rsd@plots[[1]]

Try this:

df = read.csv("Mean_SWE.csv")
# Run the model
Model = lm(formula = SWE ~ Mean.Z + Intensity.mean, data = df)
# Plot
lm.plot.rsd = autoplot(Model, label.size = 3, which = 1) +
  theme_bw()
autoplotly(lm.plot.rsd@plots[[1]]) +
  ggplot2::ggtitle("Residuals vs Fitted")