【问题标题】:Plotting hourly curves in R在 R 中绘制每小时曲线
【发布时间】:2018-09-23 16:32:53
【问题描述】:

我是 R 的初学者,想请教一下。

任务: 我想制作一个图表来表示白天每小时的用水量。该图由不同日期的几条曲线组成(例如,请参阅链接here)。

我将每天的数据分成子列表:

    > head(aaa)
    [[1]]
                   by60min  consumption
    1  2018-07-01 00:05:00            0
    2  2018-07-01 01:05:00            0
    3  2018-07-01 02:05:00            0
    4  2018-07-01 03:05:00            0
    ....
    [[2]]
                   by60min  consumption
    25 2018-07-02 00:05:00            0
    26 2018-07-02 01:05:00            0
    27 2018-07-02 02:05:00            0
    28 2018-07-02 03:05:00            0

有时,没有用水量,我想避免将这些天绘制到图表中。在这里我被卡住了。我不知道怎么做。我的想法是删除消费为零的所有天,然后绘制非零天,但我无法做到。有什么想法吗(绘制非零天或/以及如何从列表中删除子列表)?

非常感谢您。

卢博斯

补充:

# 1st step - tibble:
    aaa <- as.tibble(aaa)
    aaa
# A tibble: 1,487 x 2
    by60min             consumption
    <fct>                     <dbl>
    1 2018-07-01 00:05:00         0
    2 2018-07-01 01:05:00         0
    3 2018-07-01 02:05:00         0
    4 2018-07-01 03:05:00         0
    5 2018-07-01 04:05:00         0
    6 2018-07-01 05:05:00         0
    7 2018-07-01 06:05:00         0
    8 2018-07-01 07:05:00     0.101
    9 2018-07-01 08:05:00     0.167
   10 2018-07-01 09:05:00     0.267
   # ... with 1,477 more rows

# 2nd step - plot:
    aaa %>%
      mutate(day = factor(day(ymd_hms(by60min))),
             hour = factor(hour(ymd_hms(by60min)))) %>%
      group_by(day) %>%
      filter(sum(consumption) > 0) %>%
      ggplot(mapping = aes(x = hour, y = consumption, 
                           col = day, 
                           show.legend = FALSE)) +
      geom_line(show.legend = FALSE)

# OUTPUT (the picture below) - bar graph instead of line chart - why?
# please NOTE that akt_spotreba == consumption 

dput(aaa) # I inserted only first three rows
structure(list(by60min = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 

【问题讨论】:

  • 您好 Lubos,欢迎来到 SO。可以给a minimal, reproducible example吗?
  • 我不明白您想要的确切图表类型。在链接中,您有一个图表,其小时数从024,每月一行。线条值是每小时平均值吗?

标签: r plot curve consumption


【解决方案1】:

这是一个tidyverseapproach,使用一个简单的示例数据集,基于您提供的内容。

l1 = data.frame(by60min = c("2018-07-01 00:05:00","2018-07-01 01:05:00","2018-07-01 02:05:00"),
                consumption = 0)

l2 = data.frame(by60min = c("2018-07-02 00:05:00","2018-07-02 01:05:00","2018-07-02 02:05:00"),
                consumption = c(0,2,30))

l3 = data.frame(by60min = c("2018-07-03 00:05:00","2018-07-03 01:05:00","2018-07-03 02:05:00"),
                consumption = c(10,8,2))

l = list(l1,l2,l3)

您的原始数据如下所示:

[[1]]
by60min consumption
1 2018-07-01 00:05:00           0
2 2018-07-01 01:05:00           0
3 2018-07-01 02:05:00           0

[[2]]
by60min consumption
1 2018-07-02 00:05:00           0
2 2018-07-02 01:05:00           2
3 2018-07-02 02:05:00          30

[[3]]
by60min consumption
1 2018-07-03 00:05:00          10
2 2018-07-03 01:05:00           8
3 2018-07-03 02:05:00           2
library(tidyverse)
library(lubridate)

map_df(l, data.frame) %>%                         # combine list element to one dataframe
  mutate(day = factor(date(ymd_hms(by60min))),    # get day from date
         hr = hour(ymd_hms(by60min))) %>%         # get hour from date
  group_by(day) %>%                               # for each day
  filter(sum(consumption) > 0) %>%                # calculate sum of consumption and remove days where this is 0
  ungroup() %>%
  ggplot(aes(hr, consumption, col=day))+          # plot lines
  geom_line()

输出图:

【讨论】:

  • 哇,我印象深刻!它有效,结果正是我想要的。非常感谢,AntoniosK!
  • 安东尼奥,我想再次请你帮忙。在您向我提供解决方案之后,如果您使用 tibble 中的数据而不是列表,您的代码会怎样?非常感谢您!
  • 解决方案应该类似,因为第一个命令会从这些列表中创建一个数据框。
  • 不幸的是它不起作用。当我对传输到 tibble 的数据使用相同的代码时,它不会绘制线条而是绘制条形图。我的代码如下:mist_data2 %>% mutate(day = factor(day(ymd_hms(by60min))), hour = factor(hour(ymd_hms(by60min)))) %>% group_by(day) %>% filter(sum (消耗) > 0) %>% ggplot(mapping = aes(x = 小时, y = 消耗, col = 天, show.legend = FALSE)) + geom_line(show.legend = FALSE)
  • 我认为如果您使用您提到的数据格式(tibble)更新您的问题会更好,我会看看如何更改代码。发布代表您的实际数据集的数据示例非常重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多