-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpeak_hours.R
94 lines (69 loc) · 2.75 KB
/
peak_hours.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# peak hour and peak hours by country
library(tidyverse)
library(bigrquery)
library(scales)
library(magrittr)
library(maps)
library(lubridate)
library(ggridges)
billing <- "testproj-223217" # replace this with your project ID
sql <- "SELECT eventName, eventTimestamp, userCountry
FROM `tactile-external.interview.events`
WHERE eventName = 'gameStarted'"
#tb <- bq_project_query(billing, sql)
#df <- bq_table_download(tb)
#write_csv(df, "peak_hours.csv")
##################################################
df <- read_csv("peak_hours.csv")
df$eventTimestampMin <- hms::hms(second(df$eventTimestamp), minute(df$eventTimestamp), hour(df$eventTimestamp))
#df2 = df[1:10,]
#df2$eventTimestamp2 <- hms::hms(df2$eventTimestamp)
#df2$min <- format(df2$eventTimestamp, "%H:%M:%S")
#df2$min2 <- as.POSIXct(df2$eventTimestamp, format="%H:%M")
#df2 = df[1:100,]
#df2$eventTimestamp = hms::hms(second(df2$eventTimestamp), minute(df2$eventTimestamp), hour(df2$eventTimestamp))
#df %<>% mutate(time = as.POSIXct(strptime(df$eventTimestamp, "%Y/%m/%d %H:%M", tz = "UTC")))
# peak hours: game start
p <- df %>%
ggplot(aes(x=eventTimestampMin)) +
geom_density(fill="#9E3896")
# find peak
densities <- ggplot_build(p)[[1]][[1]]
x = densities %>%filter(density==max(density)) %>% select(x)
x = as.numeric(round(x))
xAsTime = hms::hms(x)
p +
geom_vline(xintercept=x, linetype="dashed", size = 1.5) +
annotate("text", x = x-25000, y = 1.574253e-05, label = glue::glue("Peak: {xAsTime} (UTC)"),
size=7) +
labs(x="UTC Time", y="Game starts") +
theme_light() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16))
ggsave("game_starts_time.png")
# find peak by country
countries <- read_csv("top20_countries.csv")
countryPeaks <- left_join(countries,df)
# with Hong Kong
cols <- scales::seq_gradient_pal("#C83488", "#9F3896", "Lab")(seq(0,1,length.out=length(unique(countryPeaks$region))))
countryPeaks %>%
ggplot(aes(x=eventTimestampMin,y=region,fill=region)) +
geom_density_ridges(colour="gray70") +
labs(x="UTC Time", y="") +
theme_light() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16)) +
scale_fill_manual(values=cols, guide=F)
ggsave("peak_hours_country_wHK.png")
# without Hong Kong
cols <- scales::seq_gradient_pal("#C83488", "#9F3896", "Lab")(seq(0,1,length.out=length(unique(countryPeaks$region))-1))
countryPeaks %>%
filter(userCountry!="HK") %>%
ggplot(aes(x=eventTimestampMin,y=region, fill=region)) +
geom_density_ridges(colour="gray70") +
labs(x="UTC Time", y="") +
theme_light() +
theme(axis.text = element_text(size = 14),
axis.title = element_text(size = 16)) +
scale_fill_manual(values=cols, guide=F)
ggsave("peak_hours_country.png")