Hands_on exercise 06

A new article created using the Distill format.

Tianyue Sui https://example.com/norajones (Spacely Sprockets)https://example.com/spacelysprokets
2022-05-21

Distill is a publication format for scientific and technical writing, native to the web.

Learn more about using Distill for R Markdown at https://rstudio.github.io/distill.

packages = c('clock','lubridate', 'ggthemes', 'sftime',
             'tidyverse', 'data.table', 'readr','sf','tmap','rmarkdown')

for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}
schools <- read_sf("data/wkt/Schools.csv", options = "GEOM_POSSIBLE_NAMES=location")
apartment <- read_sf("data/wkt/Apartments.csv", options = "GEOM_POSSIBLE_NAMES=location")
buildings <- read_sf("data/wkt/Buildings.csv", options = "GEOM_POSSIBLE_NAMES=location")
employers <- read_sf("data/wkt/Employers.csv", options = "GEOM_POSSIBLE_NAMES=location")
pubs <- read_sf("data/wkt/Pubs.csv", options = "GEOM_POSSIBLE_NAMES=location")
restaurant <- read_sf("data/wkt/Restaurants.csv", options = "GEOM_POSSIBLE_NAMES=location")
tmap_mode("view")   #change to "plot" could turn off interactive
tm_shape(buildings)+   #tm_shape to define data
tm_polygons(col = "grey60",   #tm_point
           size = 1,    #shade
           border.col = "black",
           border.lwd = 1)
tmap_mode("plot")  #switch off at the end, to make sure next page/ graph will not be interactive
tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "grey60",   
           size = 1,
           border.col = "black",
           border.lwd = 1) +  
tm_shape(employers) +  #the sequence is important,by layer, plot the area first,and line before point  
  tm_dots(col = "red")

logs <- read_sf("data/wkt/ParticipantStatusLogs1.csv", 
                options = "GEOM_POSSIBLE_NAMES=currentLocation")
logs_selected <- logs %>%
  mutate(Timestamp = date_time_parse(timestamp,zone = "",
                                     format = "%Y-%m-%dT%H:%M:%S"))%>%
 mutate(day = get_day(Timestamp)) %>%
  filter(currentMode == "Transport")
write_rds(logs_selected,"data/wkt/logs_selected.rds")
logs_selected<- read_rds("data/wkt/logs_selected.rds")
hex <- st_make_grid(buildings,    #use building min and max to build
                    cellsize=100, 
                    square=FALSE) %>%
  st_sf() %>%
  rowid_to_column('hex_id')
points_in_hex <- st_join(logs_selected, 
                         hex, 
                         join=st_within)
points_in_hex <- st_join(logs_selected, 
                        hex, 
                        join=st_within) %>%
  st_set_geometry(NULL) %>%   # remove geometric name
  count(name='pointCount', hex_id)    #count how many polygon point fall in hex
hex_combined <- hex %>%
  left_join(points_in_hex, 
            by = 'hex_id') %>%
  replace(is.na(.), 0)
tm_shape(hex_combined %>%
           filter(pointCount > 0))+
  tm_fill("pointCount",
          n = 8,
          style = "quantile") +
  tm_borders(alpha = 0.1)

logs_path <- logs_selected %>%
  group_by(participantId, day) %>%
  summarize(m = mean(Timestamp), 
            do_union=FALSE) %>%
  st_cast("LINESTRING") #combine all data point for particular person as a line
logs_path_selected<- logs_path %>%
  filter(participantId ==0)
tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "grey60",   
           size = 1,
           border.col = "black",
           border.lwd = 1) +  
tm_shape(logs_path_selected) +
tm_lines(col = "blue")
tmap_mode("plot")