4

I am trying to extend the lm line beyond the range of the data when plotting using geom_smooth. However, setting fullrange=TRUE doesn't seem to do the trick.

I have set the xlim beyond the data range using coord_cartesian and scale_x_log10 as shown in the code.

library(ggplot2)

    df<-data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.01,0.03,length.out=19),Treatment=2.2)
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.02,0.06,length.out=19),Treatment=2.4))
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.06,0.14,length.out=19),Treatment=2.6))
    df<-rbind(df,data.frame(Time=c(1:9 %o% 10^(2:3),10^4),Shift=seq(0.09,0.22,length.out=19),Treatment=2.8))

    myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
      scale_y_log10(breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
      scale_x_log10(breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
      labs(color="Treatment") +
      coord_cartesian(xlim=c(100,1E9),ylim=c(0.001,1))+
      theme_bw() +
      annotation_logticks() +
      geom_point()+
      geom_smooth(method="lm",fullrange=TRUE)
    plot(myPlot)

The lm line stops at the end of the data:

Stibu
  • 15,166
  • 6
  • 57
  • 71
Briebeek
  • 43
  • 5
  • Could you add information about the package or custom function that is needed for `math_format()`? – user2363777 Mar 23 '19 at 00:23
  • I apologize, I should have removed that. It is part of the scales package and is used to format the x-axis labels. The entire labels = ... part of scale_x_log10 can be removed and the problem remains. I will edit the code above and remove it. – Briebeek Mar 23 '19 at 05:07

1 Answers1

3

If the limits are set inside scale_*_log10() instead of in coord_cartesian(), the linear model is shown over the entire range:

myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
  scale_y_log10(limits = c(0.001, 1), breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
  scale_x_log10(limits = c(100, 1e9), breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
  labs(color="Treatment") +
  theme_bw() +
  annotation_logticks() +
  geom_point()+
  geom_smooth(method="lm",fullrange=TRUE)
plot(myPlot)

enter image description here

However, because setting limits on the scales remove data that lies outside of these limits, the grey error bands do not extend to the end of the lines. This can be amended by setting the limits on the scales somewhat wider and then using coord_cartesian()again:

myPlot <- ggplot(df,aes(x=Time,y=Shift,col=as.factor(Treatment))) +
  scale_y_log10(limits = c(0.001, 3), breaks = c(1 %o% 10^(-2:1)), minor_breaks = c(1:9 %o% 10^(-1:2)),labels=paste0(c(1 %o% 10^(0:3)),"%")) +
  scale_x_log10(limits = c(100, 1e9), breaks = c(1 %o% 10^(2:9)), minor_breaks = c(1:9 %o% 10^(0:10))) +
  labs(color="Treatment") +
  coord_cartesian(xlim=c(100,1E9),ylim=c(0.001,1)) +
  theme_bw() +
  annotation_logticks() +
  geom_point()+
  geom_smooth(method="lm",fullrange=TRUE)
plot(myPlot)

enter image description here

Stibu
  • 15,166
  • 6
  • 57
  • 71