首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggpmisc消除了对汇总数据长度的困惑

ggpmisc消除了对汇总数据长度的困惑
EN

Stack Overflow用户
提问于 2020-03-16 23:43:05
回答 1查看 27关注 0票数 0

我收到了这个错误消息"Error in data.frame(mean_Flow,dates):arguments differing number of rows: 84,30274“,我知道它对我想要拉取哪个日期函数感到困惑,但我不确定如何清除它。我想要将数据汇总为年度平均值。它正确地绘制了这段代码,但没有生成正确的stat_poly_eq。如果有任何帮助,我将不胜感激!

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-03-21 19:47:43

传递给stat_poly_eq()的公式在"data“中求值,就像在ggplot层中看到的那样:即,必须使用变量映射到的美学的名称,而不是使用df中的变量名称。换句话说,@Richard Telford正确地猜到了问题所在。此行为与stat_smooth()的行为相同,对于该行为,上面的代码使用formula的默认值y ~ x

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60709002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档