0

我收到此错误消息“data.frame 中的错误(mean_Flow,日期):参数暗示不同的行数:84, 30274”,我知道我希望它提取哪个日期函数但我不确定如何清除它。我希望将数据汇总为年度平均值。它正确地绘制了这个,但没有产生正确的 stat_poly_eq。我将不胜感激任何帮助!

library(dataRetrieval)
library("plyr")
siteNo = "02202500"
pCode = "00060"

daily = readNWISdv(siteNo, pCode, "1800-10-01","2020-09-30", statCd="00003")
daily = renameNWISColumns(daily)
dates= format(as.Date(daily$Date), format = "%Y")
ddply(daily, .(site_no, dates), summarise,
      mean_Flow = mean(Flow)*(0.0283168))

library(dplyr)
library(ggpmisc) # for dealing with stat equations
library(ggplot2) # for making plots 
library(lubridate) # for working with dates
library(scales) #for working with date_format

df=data.frame(mean_Flow, dates)
head(df)
df$dates = as.Date(df$dates, format = "%Y")
my.formula = df$mean_Flow ~ df$dates

p1=ggplot(data = df,aes(dates, mean_Flow)) +
  geom_line(group = 1) +
  geom_smooth(method = "lm", se=FALSE) +
  stat_poly_eq(formula = my.formula,
               eq.with.lhs = "italic(hat(y))~`=`~",
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")))+
  theme_classic()+
  labs(x="", y=(expression(Discharge~(m^{3}~s^{-1}))))+
  scale_x_date(breaks = "5 year", labels = date_format("%Y"))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
p1
4

1 回答 1

0

传递给的公式stat_poly_eq()在 ggplot 层中看到的“数据”内进行评估:即,df必须使用变量映射到的美学名称,而不是使用变量的名称。换句话说,@Richard Telford 猜对了问题所在。此行为与 的行为相同,stat_smooth()上面的代码使用 的默认y ~ xformula

于 2020-03-21T11:47:42.547 回答