Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Mapping with R
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ben Anderson
Mapping with R
Commits
22541df2
Commit
22541df2
authored
4 years ago
by
Tom Rushby
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit - working but in progress
parent
42e7567a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cartograms/hex-cartograms.R
+111
-0
111 additions, 0 deletions
cartograms/hex-cartograms.R
with
111 additions
and
0 deletions
cartograms/hex-cartograms.R
0 → 100644
+
111
−
0
View file @
22541df2
# Hex-cartograms examples ----
# https://github.com/VictimOfMaths/Maps/blob/master/WFHCartogram.R
# Using hex-map data from https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/
# required packages
library
(
curl
)
library
(
sf
)
library
(
tidyverse
)
# not (yet) required
# library(readxl)
# library(extrafont)
# library(scales)
# library(gtools)
# LA example ----
# Carl Baker's (@carlbaker) Local Authority Hex Cartogram
ltla
<-
tempfile
()
source
<-
(
"https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/raw/main/geopackages/LocalAuthorities-lowertier.gpkg"
)
ltla
<-
curl_download
(
url
=
source
,
destfile
=
ltla
,
quiet
=
FALSE
,
mode
=
"wb"
)
# List layers
st_layers
(
ltla
)
Background
<-
st_read
(
ltla
,
layer
=
"7 Background"
)
ltladata
<-
st_read
(
ltla
,
layer
=
"4 LTLA-2019"
)
Groups
<-
st_read
(
ltla
,
layer
=
"2 Groups"
)
Group_labels
<-
st_read
(
ltla
,
layer
=
"1 Group labels"
)
%>%
mutate
(
just
=
if_else
(
LabelPosit
==
"Left"
,
0
,
1
))
ggplot
()
+
geom_sf
(
data
=
Background
%>%
filter
(
Name
!=
"Ireland"
),
aes
(
geometry
=
geom
))
+
#geom_sf(data=ltladata %>% filter(response=="Total"),
# aes(geometry=geom, fill=prop), colour="Black", size=0.1)+
geom_sf
(
data
=
Groups
%>%
filter
(
RegionNation
!=
"Northern Ireland"
),
aes
(
geometry
=
geom
),
fill
=
NA
,
colour
=
"Black"
)
+
geom_sf_text
(
data
=
Group_labels
,
aes
(
geometry
=
geom
,
label
=
Group.labe
,
hjust
=
just
),
size
=
rel
(
2.4
),
colour
=
"Black"
)
+
#scale_fill_paletteer_c("pals::ocean.haline", direction=-1,
# name="Proportion ever working from home", limits=c(0,NA),
# labels=label_percent(accuracy=1))+
theme_void
()
+
theme
(
plot.title
=
element_text
(
face
=
"bold"
,
size
=
rel
(
1.5
)),
legend.position
=
"top"
)
+
guides
(
fill
=
guide_colorbar
(
title.position
=
'top'
,
title.hjust
=
.5
,
barwidth
=
unit
(
20
,
'lines'
),
barheight
=
unit
(
.5
,
'lines'
)))
+
labs
(
title
=
"Local Authorities Hex-Cartogram"
,
caption
=
"Cartogram from @carlbaker/House of Commons Library\nPlot by @VictimOfMaths"
)
# MSOA example ----
ltmsoa
<-
tempfile
()
source
<-
(
"https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/raw/main/geopackages/MSOA.gpkg"
)
ltmsoa
<-
curl_download
(
url
=
source
,
destfile
=
ltmsoa
,
quiet
=
FALSE
,
mode
=
"wb"
)
# List layers using st_layers
st_layers
(
ltmsoa
)
# Create data using st_read to read the layers we need ...
background_msoa
<-
st_read
(
ltmsoa
,
layer
=
"5 Background"
)
msoa_la_outlines
<-
st_read
(
ltmsoa
,
layer
=
"3 Local authority outlines (2019)"
)
msoa_data
<-
st_read
(
ltmsoa
,
layer
=
"4 MSOA hex"
)
msoa_groups
<-
st_read
(
ltmsoa
,
layer
=
"2 Groups"
)
msoa_group_labels
<-
st_read
(
ltmsoa
,
layer
=
"1 Group labels"
)
%>%
mutate
(
just
=
if_else
(
LabelPosit
==
"Left"
,
0
,
1
))
ggplot
()
+
geom_sf
(
data
=
background_msoa
%>%
filter
(
Name
!=
"Ireland"
),
aes
(
geometry
=
geom
))
+
#geom_sf(data=ltladata %>% filter(response=="Total"),
# aes(geometry=geom, fill=prop), colour="Black", size=0.1)+
geom_sf
(
data
=
msoa_data
%>%
filter
(
RegionNation
!=
"Northern Ireland"
),
aes
(
geometry
=
geom
),
fill
=
NA
,
colour
=
"Blue"
)
+
geom_sf
(
data
=
msoa_la_outlines
%>%
filter
(
RegionNation
!=
"Northern Ireland"
),
aes
(
geometry
=
geom
),
fill
=
NA
,
colour
=
"Black"
)
+
geom_sf
(
data
=
msoa_groups
%>%
filter
(
RegionNation
!=
"Northern Ireland"
),
aes
(
geometry
=
geom
),
fill
=
NA
,
colour
=
"Black"
)
+
geom_sf_text
(
data
=
msoa_group_labels
,
aes
(
geometry
=
geom
,
label
=
Group.labe
,
hjust
=
just
),
size
=
rel
(
2.4
),
colour
=
"Black"
)
+
#scale_fill_paletteer_c("pals::ocean.haline", direction=-1,
# name="Proportion ever working from home", limits=c(0,NA),
# labels=label_percent(accuracy=1))+
theme_void
()
+
theme
(
plot.title
=
element_text
(
face
=
"bold"
,
size
=
rel
(
1.5
)),
legend.position
=
"top"
)
+
guides
(
fill
=
guide_colorbar
(
title.position
=
'top'
,
title.hjust
=
.5
,
barwidth
=
unit
(
20
,
'lines'
),
barheight
=
unit
(
.5
,
'lines'
)))
+
labs
(
title
=
"Middle-layer Super Output Area (MSOA) Hex-Cartogram"
,
caption
=
"Cartogram from @carlbaker/House of Commons Library\nPlot by @tom_rushby"
)
# I cannot find LSOA level hex maps but here's a link with a how-to for creating hex maps
# something to try out! See ...
# https://rpubs.com/langton_/worksheet-extras-03
# https://rpubs.com/Hailstone/326118
# https://rstudio-pubs-static.s3.amazonaws.com/342278_51068843182b41ad9e00dfcc35e65247.html
# Other resources ...
# https://docs.evanodell.com/parlitools/
# ODI Leeds ...
# https://odileeds.github.io/covid-19/LocalAuthorities/hexmap.html
# https://github.com/odileeds/hexmaps
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment