r - 在同一图表中绘制不同年份的不同变量
问题描述
我有一个这样的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()
但它显示了这张图,线条没有连接,我不知道如何让它们每年都连接在一条线上。(图表是西班牙语,但请在答案中使用我用英文使用的名称,以便更多人理解我的疑问,谢谢!!!)
解决方案
你可以试试 :
library(ggplot2)
ggplot(df, aes(x = month, y = error, color = factor(year))) + geom_line()
推荐阅读
- windows-subsystem-for-linux - 在 Windows 10 上使用 bash 安装 ngspice 时遇到问题
- python - 为什么元素评估为假?
- php - array_intersect 并同时匹配多个值
- python - 对熊猫数据框进行列表理解
- typescript - 有没有办法在导入语句中放置类型断言?
- javascript - 无法使用字符串作为参数从 PHP 调用 JavaScript 函数
- javascript - 尝试将语义 UI 中的按钮放入代码中时,不断收到错误消息。错误是错误:无法在未安装的组件上找到节点
- cmake - 使用现有目标作为新但相关目标的基础
- python - 优化代码以查找下一个素数
- apache-spark - 将 SQL 逻辑“OR EXISTS(子查询)”转换为 Spark 半连接