首页 > 解决方案 > 在同一图表中绘制不同年份的不同变量

问题描述

我有一个这样的df

date           year    month    day    forecast    value    error
2018-01-01     2018    01       01     14000       13902    98
2018-01-02     2018    01       02     13290       13200    90
2018-01-03     2018    01       03     12030       12040    -10
2018-01-04     2018    01       04     13239       13239    0
2018-01-05     2018    01       05     13100       13290    -190
.
.
.
.
.
2019-01-01     2019    01       01     13247       13200    47
2019-01-02     2019    01       02     13248       13200    48
2019-01-03     2019    01       03     13240       13200    40
2019-01-04     2019    01       04     13239       13200    39
2019-01-05     2019    01       05     13100       13200    -100
.
.
.
.
.
2020-01-01     2020    01       01     14000       13902    98
2020-01-02     2020    01       02     13290       13200    90
2020-01-03     2020    01       03     12030       12040    -10
2020-01-04     2020    01       04     13239       13239    0
2020-01-05     2020    01       05     13100       13290    -190
.
.
.

我想绘制列错误,但在同一个图中不同年份。我想在同一个图中放置不同的线,一条用于 2018 年的错误,另一条用于 2019 年的错误,另一条用于 2020 年的错误,但我不知道该怎么做。

我试过用这个

ggplot(df, aes(x = month, y = error, colour = year)) + geom_line() 

但它显示了这张图,线条没有连接,我不知道如何让它们每年都连接在一条线上。(图表是西班牙语,但请在答案中使用我用英文使用的名称,以便更多人理解我的疑问,谢谢!!!)

标签: rdataframeplotgraphcorrelation

解决方案


你可以试试 :

library(ggplot2)

ggplot(df, aes(x = month, y = error, color = factor(year))) + geom_line() 

推荐阅读