【问题标题】:Plot different variables in the same graph for different years在同一图表中绘制不同年份的不同变量
【发布时间】:2021-08-05 08:25:40
【问题描述】:

我有一个这样的 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() 

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

【问题讨论】:

  • 尝试添加分组词:ggplot(df, aes(x = month, y = error, colour = factor(year), group=year)) + geom_line()

标签: r dataframe plot graph correlation


【解决方案1】:

你可以试试:

library(ggplot2)

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

【讨论】:

  • 我需要它们从 1 月到 12 月,这样它就可以用 3 种不同的颜色制作 3 条不同的线,但不能在另一条上方,它们都在同一条线上,一条由另一条继续@ RonakShah
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
相关资源
最近更新 更多