-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother_f.R
99 lines (85 loc) · 2.25 KB
/
other_f.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
95
96
97
library(tidyverse)
library(janitor)
library(ggridges)
library(leaflet)
library(plotly)
library(rvent)
library(ggiraph)
library(htmlwidgets)
library(plotly)
library(shinydashboard)
library(shinyWidgets)
library(shiny)
library(DT)
# Get data -----
all <- readRDS("c_dat.RDS")
counties <- na.omit(unique(all$county_desc))
# List for SelectInput -----
other_var <- list(
"none",
"year",
"quarter",
"light_desc",
"junction_desc",
"collision_type_desc",
"surf_cond_desc",
"lane_desc",
"rd_cond_desc",
"rd_div_desc",
"report_type",
"weather_desc",
"signal_flag_desc",
"harm_event_desc2"
)
hover_var <- names(all)[1:57]
# Authenticate for mapbox -----
map_token <- readRDS("map_token.RDS")
Sys.setenv("MAPBOX_TOKEN" = map_token) # for Orca
# Mapbox graph -----
mapbox <- function(dat = dat, variab, hover, c_lon = -72, c_lat = 40, c_zoom = 5) {
if( variab == "none") {
fig <-
dat %>%
plot_mapbox(
lat = ~latitude,
lon = ~longitude,
mode = "markers",
# color = I("grey50"),
marker = list(symbol ='circle-stroked', size=5, color='black'),
type = 'scattermapbox',
hovertext = ~dat[,hover])
} else {
fig <-
dat %>%
plot_mapbox(
lat = ~latitude,
lon = ~longitude,
mode = "markers",
color= dat[,variab],
marker = list(size=5),
type = 'scattermapbox',
hovertext = ~dat[,hover])
}
fig <- fig %>%
layout(
mapbox = list(
# style = 'open-street-map',
zoom = c_zoom,
center = list(lon = c_lon, lat = c_lat)))
fig
}
# Ist Plot ----
ist_plot <- function(dat, variab){
variab <- sym(variab)
dat %>%
group_by(!!variab) %>%
summarise(n = n()) %>%
ggplot(aes(!!variab, n)) +
geom_col(fill = "grey", col = "black") +
theme_bw() +
labs(y = "number of crashes") +
geom_label(aes(!!variab, 1, label = n)) +
scale_x_discrete(guide = guide_axis(n.dodge = 2))
}
# https://plot.ly/r/group-by/
# split = ~class