如何更改回归 ggplot 中的位置 x 轴刻度

问题描述 投票:0回答:1

完成回归方程和 F 统计后,我已经能够通过我满意的一组数据点绘制具有置信极限的回归曲线。我的 x 变量是从观察开始算起的天数,该天数恰好运行一年(2009 年 1 月 17 日至 2010 年 1 月 16 日)。生成的图的 x 轴以 50 天为间隔进行刻度,在图中以 100 天为间隔标记刻度。

我想将刻度间隔更改为第15,74,135,196,257天和318天,并标记为2月1日、4月1日、6月1日、8月1日、10月1日和12月1日。

**这是我用于回归和原始图的编码

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')

我将情节代码更改为以下内容,但几乎是靠猜测:

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)) 

当我运行此命令时,我收到以下错误报告:

> 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

**我的数据是这样开始的: **

> 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

我当然很感激帮助

我从我在中找到的代码开始 R 指定线图的 x 轴刻度

但无法弄清楚如何将其从点的线性图调整为多项式回归的平滑输出。

我不知道还能尝试什么。

r ggplot2 plot regression geom-point
1个回答
0
投票

我认为

plot()
函数是导致错误的原因。你可以使用
scale_x_continuous()
来解决这个问题,如下所示:

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')
© www.soinside.com 2019 - 2024. All rights reserved.