我想每小时绘制一次数据。我搜索了与此主题相关的所有问题,但没有成功。
他们中的大多数都告诉我们转换为POSIXct
格式。我已经做到了,但x axis
显示的是几个小时而不是几小时。
这是数据框的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"
我还希望在工作日之前完成这个图表。我已经制作了这张图来说明我的观点:
我已经做到了:
使用此代码:
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 <- 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"))
答案 0 :(得分:5)
您也可以在ggplot2之外进行计算。这可以更好地格式化,但它取决于您正在使用的ggplot2的版本(以及从哪里)。这也订了几天:
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)))
答案 1 :(得分:3)
这样的事情?
我认为(和其他评论一样)如果你把星期几和一天中的时间作为单独的列提取出来,它会让你的绘图生活更加清晰:
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")
您可以调整分配给bins = ...
的号码,以调整&#34; bins&#34;的大小。 geom_histogram
用于分组和计数,或许可以减少数量以接近纸质草图。