简体   繁体   English

R:ggplot2绘制工作日刻面的小时数据

[英]R: ggplot2 plot hourly data faceted by weekday

I want to plot data hourly. 我想每小时绘制一次数据。 I've search all the questions related to this topic, without success. 我搜索了与此主题相关的所有问题,但没有成功。

Most of them tell to transform to POSIXct format. 他们中的大多数人告诉他们转换为POSIXct格式。 I've made that but the x axis shows months not hours. 我已经做到了,但x axis显示的是几个月而不是几小时。

This is the str() for the data frame: 这是数据框的str():

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   685 obs. of  3 variables:
 $ dias  : chr  "dom" "dom" "dom" "dom" ...
 $ horas : POSIXct, format: "2016-01-03 13:45:53" ...
 $ conteo: int  1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "spec")=List of 2
  ..$ cols   :List of 3
  .. ..$ dias  : list()
  .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
  .. ..$ horas :List of 1
  .. .. ..$ format: chr ""
  .. .. ..- attr(*, "class")= chr  "collector_datetime" "collector"
  .. ..$ conteo: list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  ..$ default: list()
  .. ..- attr(*, "class")= chr  "collector_guess" "collector"
  ..- attr(*, "class")= chr "col_spec"

I would also like to facet this graph by weekday. 我还希望在工作日之前完成这个图表。 I've made this drawing to make my point: 我已经用这个图画说明了我的观点:

在此输入图像描述

I've made this: 我做了这个:

在此输入图像描述

With this code: 使用此代码:

ggplot(data=data,aes(x=horas, y=conteo)) +
  #geom_point() +
  geom_bar(colour = "blue",stat = "identity") +
  #facet_wrap(~ dias) +
  ylab("Sismos") + 
  xlab("Hora") +
  #opts(title = "Precipitacion acumulada horaria \n 2008-05-27 Burriana") +
  scale_y_continuous(limits = c(0,2)) 

Data: 数据:

data <- structure(list(dias = c("dom", "dom", "dom", "dom", "dom", "dom", 
                          "dom", "dom", "dom", "jue", "jue", "jue", "jue", "jue", "jue", 
                          "jue", "jue", "jue", "jue", "lun", "lun", "lun", "lun", "lun", 
                          "lun", "lun", "lun", "lun", "lun", "mar", "mar", "mar", "mar", 
                          "mar", "mar", "mar", "mar", "mar", "mar", "mar", "mi<e9>", "mi<e9>", 
                          "mi<e9>", "mi<e9>", "mi<e9>", "mi<e9>", "mi<e9>", "mi<e9>", "mi<e9>", 
                          "mi<e9>", "mi<e9>", "mi<e9>", "s<e1>b", "s<e1>b", "s<e1>b", "s<e1>b", 
                          "s<e1>b", "s<e1>b", "s<e1>b", "s<e1>b", "s<e1>b", "s<e1>b", "s<e1>b", 
                          "s<e1>b", "s<e1>b", "s<e1>b", "vie", "vie", "vie", "vie", "vie", 
                          "vie", "vie", "vie", "vie", "vie", "vie", "vie", "vie"), horas = c("03/01/2016 13:45", 
                                                                                             "10/01/2016 03:57", "10/01/2016 08:22", "10/01/2016 15:43", "18/09/2016 07:05", 
                                                                                             "18/09/2016 12:37", "25/09/2016 00:09", "25/09/2016 07:10", "25/09/2016 11:02", 
                                                                                             "31/12/2015 21:26", "31/12/2015 23:18", "07/01/2016 09:55", "07/01/2016 21:17", 
                                                                                             "07/01/2016 22:14", "14/01/2016 01:05", "14/01/2016 02:35", "14/01/2016 12:43", 
                                                                                             "14/01/2016 13:30", "21/01/2016 06:44", "04/01/2016 11:36", "04/01/2016 14:01", 
                                                                                             "04/01/2016 20:51", "04/01/2016 21:25", "04/01/2016 22:53", "11/01/2016 04:58", 
                                                                                             "11/01/2016 17:23", "18/01/2016 20:11", "18/01/2016 21:04", "18/01/2016 22:28", 
                                                                                             "05/01/2016 00:14", "05/01/2016 01:23", "05/01/2016 03:22", "05/01/2016 04:45", 
                                                                                             "05/01/2016 21:00", "05/01/2016 21:13", "12/01/2016 06:50", "12/01/2016 14:12", 
                                                                                             "19/01/2016 00:45", "19/01/2016 03:28", "19/01/2016 07:52", "13/01/2016 02:09", 
                                                                                             "13/01/2016 02:30", "13/01/2016 02:52", "13/01/2016 03:22", "13/01/2016 04:02", 
                                                                                             "13/01/2016 05:41", "13/01/2016 07:20", "13/01/2016 08:45", "13/01/2016 15:05", 
                                                                                             "20/01/2016 07:01", "20/01/2016 18:20", "27/01/2016 00:49", "09/01/2016 21:19", 
                                                                                             "09/01/2016 22:29", "16/01/2016 00:25", "16/01/2016 05:28", "16/01/2016 05:59", 
                                                                                             "16/01/2016 16:39", "23/01/2016 02:31", "23/01/2016 03:46", "23/01/2016 19:51", 
                                                                                             "30/01/2016 08:04", "30/01/2016 11:03", "30/01/2016 14:55", "06/02/2016 01:20", 
                                                                                             "20/02/2016 22:51", "22/01/2016 04:19", "22/01/2016 14:11", "29/01/2016 03:41", 
                                                                                             "29/01/2016 11:06", "29/01/2016 11:37", "29/01/2016 12:27", "05/02/2016 12:44", 
                                                                                             "12/02/2016 01:50", "12/02/2016 08:11", "12/02/2016 22:46", "12/02/2016 23:32", 
                                                                                             "19/02/2016 11:27", "19/02/2016 18:27"), conteo = c(1L, 1L, 1L, 
                                                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = c("tbl_df", 
                                                                                                                                                                                                             "tbl", "data.frame"), row.names = c(NA, -79L), .Names = c("dias", 
                                                                                                                                                                                                                                                                       "horas", "conteo"), spec = structure(list(cols = structure(list(
                                                                                                                                                                                                                                                                         dias = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                            "collector")), horas = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                                                                               "collector")), conteo = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                                                                                                                                   "collector"))), .Names = c("dias", "horas", "conteo")), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 "collector"))), .Names = c("cols", "default"), class = "col_spec"))

You can do the calculations outside of ggplot2 as well. 您也可以在ggplot2之外进行计算。 This can be formatted better but it depends on the version of ggplot2 you are using (and from where). 这可以更好地格式化,但它取决于您正在使用的ggplot2的版本(以及从哪里)。 This also orders the days: 这也订了几天:

library(tidyverse)
library(ggplot2)

# `data` is a super-bad name for a variable so I used `df` which is
# only marginally better. ideally, you'd use something far more 
# descriptive so your future self doesn't hate you.

mutate(df, dias=ifelse(dias=="mi<e9>", "mie", dias)) %>%       # needed to do this on my system with your `dput()`
  mutate(dias=ifelse(dias=="s<e1>b", "sab", dias)) %>%         # needed to do this on my system with your `dput()`
  mutate(dias=factor(dias, levels=c("lun", "mar", "mie", "jue", "vie", "sab", "dom"))) %>%  # ordered days; i'd personally start them on Sunday but I have no idea what you need
  mutate(horas=as.POSIXct(horas, format="%d/%m/%Y %H:%M")) %>% 
  mutate(hour=lubridate::hour(horas)) -> df

count(df, dias, hour, wt=conteo) %>% 
  ggplot(aes(hour, n)) +
  geom_segment(aes(xend=hour, yend=0)) +
  geom_point() +
  scale_x_continuous(breaks=c(0, 12, 23),
                     labels=c("00:00", "12:00", "23:00"),
                     limits=c(0,23)) +
  facet_wrap(~dias, scales="free_x") +
  labs(x=NULL, y=NULL) +
  theme_minimal() +
  theme(strip.text=element_text(hjust=0, face="bold")) +
  theme(panel.grid.major.x=element_blank()) +
  theme(panel.grid.minor=element_blank()) +
  theme(plot.margin=margin(30,30,30,30)) +
  theme(axis.text.x=element_text(hjust=c(0, 0.5, 1)))

在此输入图像描述

something like this? 这样的事情?

I think (like the other comments) if you go about extracting the day of the week and the time of day as separate columns, it will make your plotting life a little clearer: 我认为(就像其他评论一样)如果你将一周中的某一天和一天中的时间作为单独的列提取出来,它将使你的绘图生活更加清晰:

library(lubridate)
library(ggplot2)
library(dplyr)
library(magrittr)

data %<>% mutate(horas = dmy_hm(horas)) # convert to a time class
data %<>% mutate(reloj = hour(horas) + minute(horas)/60) # seperate time of day
data %<>% mutate(dias = wday(horas, label = T)) # seperate day of week

ggplot(data=data,aes(x=reloj)) +
    geom_histogram(colour = "blue",bins = 20) + # change from geom_bar (drop y aesthetic)
    facet_wrap(~ dias) +
    ylab("Sismos") + 
    xlab("Hora")

在此输入图像描述

You can adjust the number assigned to bins = ... to adjust the size of the "bins" that geom_histogram is using for bucketing and counting, perhaps reduce the number to get closer to your paper sketch. 您可以调整分配给bins = ...的数字,以调整geom_histogram用于geom_histogram和计数的“分档”的大小,也许可以减少数量以接近纸质草图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM