问题描述
我正在尝试绘制此图的趋势线(附在下面)。我通过组合两个数据帧(df_year1 和 df_year2)创建了一个新的数据帧(bothyearsdf)。它们具有相同的列名但不同的行值。我将第一列的类从“字符”更改为“日期”,因为它显示了 2019 年和 2020 年的所有天数 (%y/%m/%d)。我保留了原来的另一列,它是数字。
class(bothyearsdf$Data.first_max_value) #"numeric"
class(bothyearsdf$Data.date_local) #"character"
bothyearsdf$Data.date_local <- as.Date(df_year2$Data.date_local,format='%y/%m/%d')
class(bothyearsdf$Data.date_local)"date"
然后,我尝试单独使用 geom_smooth-geom_smooth()-来创建该行,但起初它给了我一个错误,“FUN(X[[i]],...) 中的错误:对象”未找到 Data.date_local'。”然后,我使用了下面的代码,它停止给我一个错误,但它没有在图上形成趋势线。它还说“geom_smooth()` using method = 'loess' and formula 'y ~ x'”
提前谢谢你!!
w <- ggplot(NULL,aes(x=Data.date_local,y=Data.first_max_value)) +
geom_point(data = bothyearsdf) +
geom_smooth(data = bothyearsdf,aes(x = Data.date_local,y =
Data.first_max_value,color = 'red')) +
labs(x = "Days",y = "PM2.5 Max Value",title = "Wake County,NC Max PM2.5
Value for 2019 and 2020")
w
'''
解决方法
如果你尝试,你会得到什么:
w <- ggplot(bothyearsdf,aes(x = as.Date(Data.date_local),y = Data.first_max_value)) +
geom_point() +
geom_smooth(color = 'red') +
labs(x = "Days",y = "PM2.5 Max Value",title = "Wake County,NC Max PM2.5 Value for 2019 and 2020")
w