R语言 ggplot不同时间不同类别的线图

wpx232ag  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(106)

我试图创建一个每小时的线图,每15分钟收集一次数据,如下所示:

#load necessary libraries
library(tidyverse)
library(dplyr)
library(ggplot2)

DT <- data.frame(periods=c("7:15- 7:30","7:30- 7:45","7:45- 8:00",
                           "8:00-8:15","8:15-8:30","8:30-8:45","8:45-9:00",
                           "9:00-9:15","9:15-9:30","9:30-9:45","9:45-10:00", "10:00-10:15"),
                 Blue=c(2,4,6,4,11,3,4,6,3,3,9, 19),
                 Red=c(4,5,3,4,7,7,7,9,10,3,10, 12 ),
                 Yellow=c(1,0,6,8,1,6,4,5,3,1,3,2),
                 stringsAsFactors=FALSE)
DT

The code I tried is as below:

#load necessary libraries
library(ggplot2)
library(reshape2)

#melt data frame into long format
df <- melt(DT ,  id.vars = 'periods', variable.name = 'colors')

#create line plot for each column in data frame
ggplot(df, aes(periods, value)) +
  geom_line(aes(colour = colors))

字符串
我想线对应不同的类别或列与不同的颜色和x轴对应的时间段。我还想总结一下4个“15分钟时段”来组成一个小时。
我该如何使用ggplot或plotly?
亲切地

zu0ti5jz

zu0ti5jz1#

我有一个解决方案,但我不得不承认这不是我最好的作品。第一步是准备时间段数据。为此,我将从periods列中提取开始时间。然后从结果POSIXct-对象中获取小时值,并构造一个小时时间间隔字符串。

df <-
  DT %>% mutate(
    startTime = as.POSIXct(trimws(sub("-.*", "", periods)), format = "%H:%M"),
    hour = as.numeric(format(startTime, format = "%H")),
    interval = paste0(
      str_pad(hour, 2, "left", "0"),
      ":00-",
      str_pad(hour + 1, 2, "left", "0"),
      ":00"
    )
  )
> df
       periods Blue Red Yellow           startTime hour    interval
1   7:15- 7:30    2   4      1 2023-07-18 07:15:00    7 07:00-08:00
2   7:30- 7:45    4   5      0 2023-07-18 07:30:00    7 07:00-08:00
3   7:45- 8:00    6   3      6 2023-07-18 07:45:00    7 07:00-08:00
4    8:00-8:15    4   4      8 2023-07-18 08:00:00    8 08:00-09:00
5    8:15-8:30   11   7      1 2023-07-18 08:15:00    8 08:00-09:00
6    8:30-8:45    3   7      6 2023-07-18 08:30:00    8 08:00-09:00
7    8:45-9:00    4   7      4 2023-07-18 08:45:00    8 08:00-09:00
8    9:00-9:15    6   9      5 2023-07-18 09:00:00    9 09:00-10:00
9    9:15-9:30    3  10      3 2023-07-18 09:15:00    9 09:00-10:00
10   9:30-9:45    3   3      1 2023-07-18 09:30:00    9 09:00-10:00
11  9:45-10:00    9  10      3 2023-07-18 09:45:00    9 09:00-10:00
12 10:00-10:15   19  12      2 2023-07-18 10:00:00   10 10:00-11:00

然后,我们可以删除startTimehourperiods-列,并在按interval和名称分组并对值求和之前将 Dataframe 转换为长格式

df <-
  df %>% select(-c(startTime, hour, periods)) %>% pivot_longer(!interval) %>%
  group_by(interval, name) %>% summarize(valueSum = sum(value))
> df
# A tibble: 12 × 3
# Groups:   interval [4]
   interval    name   valueSum
   <chr>       <chr>     <dbl>
 1 07:00-08:00 Blue         12
 2 07:00-08:00 Red          12
 3 07:00-08:00 Yellow        7
 4 08:00-09:00 Blue         22
 5 08:00-09:00 Red          25
 6 08:00-09:00 Yellow       19
 7 09:00-10:00 Blue         21
 8 09:00-10:00 Red          32
 9 09:00-10:00 Yellow       12
10 10:00-11:00 Blue         19
11 10:00-11:00 Red          12
12 10:00-11:00 Yellow        2

现在可以绘制此数据。使用间隔作为x轴,您需要为geom_line()指定group-美学,因为我们在离散轴上绘制一条直线:

ggplot(df, aes(interval, valueSum)) +
  geom_line(aes(colour = name, group = name))


x1c 0d1x的数据
编辑:
您可以跳过转换为时间对象,直接从periods字符串中获取小时:

df <-
  DT %>% mutate(
    hour = as.numeric(trimws(sub(":.*", "", periods))),
    interval = paste0(
      str_pad(hour, 2, "left", "0"),
      ":00-",
      str_pad(hour + 1, 2, "left", "0"),
      ":00"
    )
  )

df <-
  df %>% select(-c( hour, periods)) %>% pivot_longer(!interval) %>%
  group_by(interval, name) %>% summarize(valueSum = sum(value))

ggplot(df, aes(interval, valueSum)) +
  geom_line(aes(colour = name, group = name))

相关问题