-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro-to-r.R
54 lines (38 loc) · 1.38 KB
/
intro-to-r.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
# Basic R:
x = 1:5
y = c(0, 1, 3, 9, 18)
plot(x, y)
class(x)
# from c. 7: https://itsleeds.github.io/rrsrr/space.html
library(sf) # load the sf package for working with spatial data
library(tidyverse) # load the tidyverse as before
crashes = tibble(
casualty_type = c("pedestrian", "cyclist", "cat"),
casualty_age = seq(from = 20, to = 60, by = 20),
vehicle_type = c("car", "bus", "tank"),
dark = c(TRUE, FALSE, TRUE)
)
crashes_sf = crashes # create copy of crashes dataset
crashes_sf$longitude = c(-1.3, -1.2, -1.1)
crashes_sf$latitude = c(50.7, 50.7, 50.68)
crashes_sf = st_as_sf(crashes_sf, coords = c("longitude", "latitude"), crs = 4326) # st_as_sf converts longitude and latitude coordinates into spatial objects using a specified Coordinate Reference System (see 7.6)
zones = pct::get_pct_zones("isle-of-wight")[1:9]
plot(zones$geometry) # plot just the geometry of one layer
plot(zones_containing_crashes$geometry, col = "grey", add = TRUE)
# From Geocomputation with R:
pkgs = c(
"sf",
"tidyverse",
"stplanr",
"tmap"
)
install.packages("sf")
remotes::install_cran(pkgs)
u = "https://npttile.vs.mythic-beasts.com/npct-data/pct-outputs-regional-notR/commute/msoa/west-yorkshire/z.geojson"
zones = sf::read_sf(u)
plot(zones)
library(tmap)
tmap_mode("view")
tm_shape(zones) + tm_polygons()
# see https://geocompr.robinlovelace.net/adv-map.html
# for more on map making