0

Here is the subset of data from my dataframe which is used in this plot;

Year region gear Species.Code query LPUE 1974 Cyprus creel LOB Pre 0.31 1975 Cyprus creel LOB Pre 0.26 1976 Cyprus creel LOB Pre 0.33 1977 Cyprus creel LOB Pre 0.17 1978 Cyprus creel LOB Pre 0.2 1979 Cyprus creel LOB Pre 0.22 1980 Cyprus creel LOB Pre 0.38 1981 Cyprus creel LOB Pre 0.51 1982 Cyprus creel LOB Pre 0.57 1983 Cyprus creel LOB Pre 0.45 1984 Cyprus creel LOB Post 0.43 1985 Cyprus creel LOB Post 0.33 1986 Cyprus creel LOB Post 0.21 1987 Cyprus creel LOB Post 0.69 1988 Cyprus creel LOB Post 0.65 1989 Cyprus creel LOB Post 0.37 1990 Cyprus creel LOB Post 0.35 1991 Cyprus creel LOB Post 0.15 1992 Cyprus creel LOB Post 0.21 1993 Cyprus creel LOB Post 0.17 

I have generated a line plot of time-series data and fitted two linear regressions one for the data up to 1984 and one for the data after 1984. Using the following code;

ggplot(subset(A7,region=="Cyprus"&gear=="creel"&Species.Code=="LOB"),aes(x=Year,y=LPUE,shape=query))+ geom_line()+ geom_smooth(method="lm")+ theme(panel.background = element_rect(fill = 'white', colour = 'black')) 

I would like to know firstly how I can print the equations on the plot (other examples I have found on stack overflow only deal with one lm) and secondly how I can save the lm models so that I can do a statistical test of the significance between them.

3
  • A screen shot of what you have would help as you probably didn't include enough data to reproduce what you have. Commented Oct 6, 2015 at 10:53
  • If you want to do statistical tests, I'd suggest to fit the models outside of ggplot. Commented Oct 6, 2015 at 11:10
  • sorry @MikeWise i'm not familiar with how to add a screen shot. I can only see how to add images from the internet. Commented Oct 6, 2015 at 11:16

1 Answer 1

2

you can add several regression lines to a ggplot by stacking geom_smooth commands. I would suggest to create separate data frames for the two models you want to fit.

data_upto84 <- subset(A7, year<1985) data_from84 <- subset(A7, year>1984) ggplot(data_upto84, aes(YEAR, LPUE)) + stat_summary(fun.data=mean_cl_normal) + geom_smooth(method = 'lm') + geom_smooth(data = data_from84, aes(YEAR, LPUE), method = 'lm') 

On your second question: "secondly how I can save the lm models so that I can do a statistical test of the significance between them". You save a model by assigning it to an object:

model1 <- lm(LPUE ~ 1 + YEAR, data = data_upto84) model2 <- lm(LPUE ~ 1 + YEAR, data = data_from84) 

It is not clear to me what you want to test with these models. Standard model comparison (for example using the anova function) will not be valid as you run the models on two different samples.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.