gpt4 book ai didi

r - 使用多个组在 R ggplot 中绘制时间序列

转载 作者:行者123 更新时间:2023-12-04 01:36:56 25 4
gpt4 key购买 nike

这是一个最小的工作示例:

nat <- structure(list(X = 1:336, value = c(56, 52, 48, 44, 43, 44, 37, 
36, 29, 27, 15, 22, 36, 22, 59, 53, 49, 45, 44, 44, 38, 37, 30,
27, 16, 22, 35, 21, 61.1, 54.6, 49.9, 46.6, 45.6, 45, 39.7, 37.7,
30.7, 28.6, 15.9, 22.7, 37.7, 22, 59.6, 52, 47.9, 44.8, 43.5,
43.4, 38, 36.5, 29.5, 27.4, 15, 21.6, 36.7, 21.4, 59.4, 52.5,
48.4, 45.1, 44, 44.2, 38.5, 37.3, 30, 27.9, 15.3, 22.1, 36.8,
22.5, 59.7, 53, 48.2, 44.8, 43.5, 43.7, 38.5, 37.1, 29.6, 27.8,
15.1, 21.6, 37, 22, 60, 52.1, 47, 44.4, 42.7, 43.1, 37.9, 36.3,
29.1, 27.2, 14.5, 20.9, 35.7, 21.4, 62.4, 55.1, 49, 46.4, 45,
45.2, 40.2, 38.4, 30.7, 28.6, 15.2, 21.9, 37.7, 22.6, 61.8, 55.6,
49.5, 46.4, 45.7, 45.6, 39.6, 38, 30.8, 28.6, 15.2, 22.3, 37.7,
22.6, 62.7, 56.2, 49.9, 46.6, 45.7, 45.6, 39.7, 38.3, 30.6, 28.4,
15.4, 22.2, 37.9, 23.4, 68.1, 60.8, 54.7, 50.5, 50.1, 49.1, 44.1,
43, 33.9, 31.4, 17.5, 25, 40.2, 26.1, 66.4, 59.7, 53.7, 49.5,
48.8, 47.7, 43.2, 41.7, 32.7, 30.2, 16.7, 23.8, 38.8, 25.4, 63.1,
58.2, 52.5, 48.5, 47.5, 46.6, 41.5, 40.7, 31.7, 29.4, 16.1, 23,
37.6, 23.8, 67.5, 61.2, 55.5, 51.2, 50.1, 49.5, 44.5, 43.8, 34,
31.7, 18, 24.9, 40.9, 26.6, 68.5, 61.3, 56.3, 51.3, 50.6, 50,
44.6, 44, 34.4, 31.8, 18.1, 25, 41, 26.6, 70.3, 62.2, 56.9, 52.5,
51.3, 50.8, 45.5, 44.6, 34.8, 32.2, 18.6, 24.9, 40.7, 26.7, 69,
60.7, 55.8, 51.5, 50, 50.7, 44.6, 44, 34.4, 31.6, 18.1, 24.3,
40.2, 25.9, 65.9, 59.4, 53.9, 49.6, 48.5, 49.4, 42.7, 42.3, 33.3,
30.2, 17.2, 23, 38.2, 24.2, 68.4, 61.9, 56.7, 52.5, 51.7, 51.8,
45.1, 44.8, 35.4, 32.1, 18.9, 24.7, 41, 26.5, 69.7, 62.6, 56.8,
52.6, 51.4, 51.9, 45.7, 44.7, 35.8, 32.2, 19, 25.2, 41.2, 26.5,
67.8, 61.3, 55.7, 51.5, 50.8, 50.2, 44.6, 43.9, 35, 31.6, 17.9,
24.4, 41.4, 26.8, 70.9, 63.4, 57.2, 53.5, 52.1, 51.3, 45.9, 45.1,
36.5, 32.9, 19, 25.6, 42.9, 27.2, 70.4, 62.4, 56.9, 52.6, 51.4,
50.5, 45.1, 44.8, 36.1, 32.6, 18.7, 25.3, 42.6, 27.1, 70.1, 63.2,
57, 52.6, 51.1, 49.7, 45.1, 44.3, 36.2, 32.4, 18.5, 25.1, 41.6,
27.1), date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L), .Label = c("2019_11_06", "2019_11_07",
"2019_11_08", "2019_11_09", "2019_11_10", "2019_11_11", "2019_11_12",
"2019_11_13", "2019_11_14", "2019_11_15", "2019_11_16", "2019_11_17",
"2019_11_18", "2019_11_19", "2019_11_20", "2019_11_21", "2019_11_22",
"2019_11_23", "2019_11_24", "2019_11_25", "2019_11_26", "2019_11_27",
"2019_11_28", "2019_11_29"), class = "factor"), group = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("a",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"
), class = "factor")), class = "data.frame", row.names = c(NA,
-336L))

ggplot(data=nat) +
geom_line(aes(x=date, y = value,colour=group))


这是输出:

enter image description here
https://i.imgur.com/3G9h4ZG.png

我看过一些推荐的帖子:
  • combine time series plot by using R
  • Plot a time series grouped by id
  • Plotting time series with R
  • Time series multiple plot for different group in R
  • Plotting multiple time-series in ggplot

  • 并且无法从他们那里找到解决方案。

    我期待多条线按组着色。

    例如(这不代表我想要的样式,只是一个带有几条按组着色的线条的图):

    enter image description here

    最佳答案

    您可以尝试这样的操作,我建议您将日期转换为日期,例如使用 lubridate::ymd() :

    library(tidyverse)
    library(lubridate)
    # your data
    nat %>%
    # add date as date
    mutate(date = ymd(date)) %>%
    # plot them
    ggplot( aes(x = date, y = value, color = group, group = group)) +
    geom_line() + geom_point() + theme_test()

    enter image description here

    关于r - 使用多个组在 R ggplot 中绘制时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195398/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com