get_mob_matrix: Get mobility matrix from longform data

Description Usage Arguments Value Author(s) See Also Examples

View source: R/utils.R

Description

Takes X and Y coordinates of two locations and returns cross distance for all entries.

Usage

1
get_mob_matrix(orig, dest, value, missing_obs = "NA")

Arguments

orig

vector of origin names

dest

vector of destination names

value

vector of observed values for each origin/destination pair

missing_obs

filler value for missing observations (default = NA)

Value

named numeric matrix

Author(s)

John Giles

See Also

Other data synthesis: get_crossdist(), get_distance_matrix(), get_stay_data()

Examples

 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
x <- travel_data_sim

x <- cbind(x, get_unique_ids(x, adm_start=2))

M <- get_mob_matrix(orig=x$orig_id,
                    dest=x$dest_id,
                    value=x$trips)

library(ggplot2)
ggplot(data=reshape2::melt(M)) +
  geom_tile(aes(x=factor(destination),
                y=factor(origin),
                fill=value)) +
  xlab('Destination') + ylab("Origin") +
  theme_bw() + theme(axis.text.x=element_text(size=10),
                     axis.text.y=element_text(size=10),
                     axis.title.x=element_text(size=12, margin = margin(t = 15)),
                     axis.title.y=element_text(size=12, margin = margin(r = 15)),
                     legend.position='bottom') +
  viridis::scale_fill_viridis(option='inferno', direction=1) +
  guides(fill=guide_colorbar(title='Observed trips',
                             title.position='top',
                             label.theme=element_text(size=9),
                             barwidth=20,
                             barheight=0.5,
                             frame.colour='black',
                             ticks=TRUE))

COVID-19-Mobility-Data-Network/mobility documentation built on Nov. 22, 2021, 12:17 a.m.