Search code examples
rggplot2plotregressiongeom-point

How can I change position x-axis ticks in ggplot of regression


Having completed my regression equation and F-statistic I have been able to plot a regression curve with confidence limits through my set of data points which I am happy with. My x variable is number of days from start of observations which runs for exactly one year (17 Jan 2009 to 16 Jan 2010). The resulting plot has an x-axis with ticks at 50 day intervals with scale marked at 100 day intervals in figures.

I would like to change the tick intervals to days 15,74,135,196,257 and 318 and marked 1 Feb, 1 Apr, 1 Jun, 1 Aug, 1 Oct and 1 Dec.

**This is the coding I used for the regression and original plot

frpd<-read_csv("frpd.csv")
ggplot(frpd, aes(x=Days, y=cover))+geom_point()
frpd.shuffled<-frpd[sample(nrow(frpd)),]
K<-10
degree<-5
folds<-cut(seq(1,nrow(frpd.shuffled)),breaks=K, labels=FALSE)
mse=matrix(data=NA,nrow=K,ncol=degree)
for(i in 1:K){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    testData <- frpd.shuffled[testIndexes, ]
    trainData <- frpd.shuffled[-testIndexes, ]
    for (j in 1:degree){
      fit.train = lm(cover ~ poly(Days,j), data=trainData)
      fit.test = predict(fit.train, newdata=testData)
      mse[i,j] = mean((fit.test-testData$cover)^2) 
    }
}
colMeans(mse)
best = lm(cover ~ poly(Days,2, raw=T), data=frpd)
summary(best)
lm(formula=cover~poly(Days,2,raw=T),data=frpd)
ggplot(frus, aes(x=Days, y=cover)) + 
  geom_point() +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1) + 
  xlab('Days') +
  ylab('cover')

I changed the plot code to the following but pretty much by guess work:

ggplot(frpd, aes(x=Days, y=cover)) 
  geom_point() + 
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1) +
    geom_point(mapping = frpd,x=c(15,74,135,196,257,318),y=cover, xaxt='n')+
    ylab('cover')+
    xlab('date')+
plot(axis(1,at=c(15,74,135,196,257,318)),labels=c("1 Feb", "1 Apr", "1 Jun", "1 Aug", "1 Oct", "1 Dec"))
scale_y_continuous(limits = c(0,100), breaks=seq(0,100,20)) 

When I ran this I got the following error report:

> ggplot(frpd, aes(x=Days**, y=cover)) 
> geom_point() + 
+     stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1) +
+     geom_point(mapping = frpd,x=c(15,74,135,196,257,318),y=cover, xaxt='n')+
+     ylab('cover')+
+     xlab('date')+
+     plot(axis(1,at=c(15,74,135,196,257,318)),labels=c("1 Feb", "1 Apr", "1 Jun", "1 Aug", "1 Oct", "1 Dec"))
Error in `+.gg`:
! Cannot add <ggproto> objects together
ℹ Did you forget to add this object to a <ggplot> object?
Run `rlang::last_trace()` to see where the error occurred.
> scale_y_continuous(limits = c(0,100), breaks=seq(0,100,20)) 
<ScaleContinuousPosition>
 Range:  
 Limits:    0 --  100

**My data starts like this: **

> frpd
# A tibble: 72 × 3
    Date  Days cover
   <dbl> <dbl> <dbl>
 1 39863    49   100
 2 39885    71   100
 3 39966   152    50
 4 40065   251    13
 5 40109   295    25
 6 40151   337    25
 7 40196   382    25
 8 39870    56   100
 9 39885    71   100
10 39935   121    17

I would certainly appreciate help

I started with the code I found in R specifying x-axis ticks for a line plot

But could not figure how to adapt it from a linear plot of points to smoothed output of the polynomial regression.

I'm not sure what else to try.

Images of plots: 1 Original with full confidence limits overlay

2 Modified with reduced CL range


Solution

  • i think the plot() function is what causes the error. you could use scale_x_continuous() to fix that like this:

    ggplot(frpd, aes(x = Days, y = cover)) +
      geom_point() +
      stat_smooth(method = 'lm', formula = y ~ poly(x, 2), size = 1) +
      scale_x_continuous(breaks = c(15, 74, 135, 196, 257, 318),
                         labels = c("1 Feb", "1 Apr", "1 Jun", "1 Aug", "1 Oct", "1 Dec")) +
      scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 20)) +
      ylab('cover') +
      xlab('date')