Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
woRkflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
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
Tom Rushby
woRkflow
Commits
67c942c8
Commit
67c942c8
authored
4 years ago
by
Tom Rushby
Browse files
Options
Downloads
Patches
Plain Diff
Add batch plotting and aggregation.
parent
3f8ff2ba
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
howTo/openGeogAPI/local_auth_ghg_plots.R
+60
-30
60 additions, 30 deletions
howTo/openGeogAPI/local_auth_ghg_plots.R
with
60 additions
and
30 deletions
howTo/openGeogAPI/local_auth_ghg_plots.R
+
60
−
30
View file @
67c942c8
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
# Load libraries ----
# Load libraries ----
library
(
here
)
library
(
readxl
)
library
(
readxl
)
library
(
ggplot2
)
library
(
ggplot2
)
library
(
plotly
)
library
(
plotly
)
...
@@ -85,12 +85,26 @@ per_capita_dt <- dt %>%
...
@@ -85,12 +85,26 @@ per_capita_dt <- dt %>%
rename
(
Population
=
`Population ('000s, mid-year estimate)`
)
%>%
rename
(
Population
=
`Population ('000s, mid-year estimate)`
)
%>%
mutate
(
Population
=
Population
*
1000
)
mutate
(
Population
=
Population
*
1000
)
total_hampshire_dt
<-
dt
%>%
filter
(
Name
%in%
las_to_load
)
%>%
group_by
(
Year
)
%>%
summarise_if
(
is.numeric
,
sum
,
na.rm
=
TRUE
)
%>%
mutate
(
Name
=
"Hampshire"
)
per_cap_fun
<-
function
(
x
)
x
*
1000
/
per_capita_dt
$
Population
per_cap_fun
<-
function
(
x
)
x
*
1000
/
per_capita_dt
$
Population
per_capita_totals
<-
data.frame
(
per_capita_dt
[
c
(
1
:
5
,
30
)],
lapply
(
per_capita_dt
[
c
(
11
,
15
,
21
,
28
,
29
,
32
,
33
)],
per_cap_fun
)
)
per_capita_totals
<-
data.frame
(
per_capita_dt
[
c
(
1
:
5
,
30
)],
lapply
(
per_capita_dt
[
c
(
11
,
15
,
21
,
28
,
29
,
32
,
33
)],
per_cap_fun
)
)
per_capita_detail
<-
data.frame
(
per_capita_dt
[
c
(
1
:
5
,
30
)],
lapply
(
per_capita_dt
[
c
(
6
:
10
,
12
:
14
,
16
:
20
,
22
:
27
,
29
,
32
,
33
)],
per_cap_fun
)
)
per_capita_detail
<-
data.frame
(
per_capita_dt
[
c
(
1
:
5
,
30
)],
lapply
(
per_capita_dt
[
c
(
6
:
10
,
12
:
14
,
16
:
20
,
22
:
27
,
29
,
32
,
33
)],
per_cap_fun
)
)
# Note that for hampshire LAs there are no emissions in categories Q or S
# Note that for hampshire LAs there are no emissions in categories Q or S
# Create plot tables ----
pc_detail_plot
<-
data.frame
(
per_capita_detail
[
c
(
1
:
5
,
7
:
25
)])
pc_detail_plot
<-
melt
(
pc_detail_plot
,
id.vars
=
c
(
"CTRY18NM.RGN18NM"
,
"Second.Tier.Authority"
,
"Name"
,
"Code"
,
"Year"
))
pc_general_plot
<-
data.frame
(
per_capita_totals
[
c
(
3
,
5
,
11
:
13
)])
# Calculate emissions and removals for LULUCF separately
# Calculate emissions and removals for LULUCF separately
per_capita_lulucf
<-
pc_detail_plot
%>%
per_capita_lulucf
<-
pc_detail_plot
%>%
filter
(
grepl
(
"LULUCF"
,
variable
))
%>%
filter
(
grepl
(
"LULUCF"
,
variable
))
%>%
...
@@ -101,15 +115,9 @@ per_capita_lulucf <- pc_detail_plot %>%
...
@@ -101,15 +115,9 @@ per_capita_lulucf <- pc_detail_plot %>%
per_capita_totals
<-
left_join
(
per_capita_totals
,
per_capita_lulucf
,
by
=
c
(
"Name"
,
"Year"
))
per_capita_totals
<-
left_join
(
per_capita_totals
,
per_capita_lulucf
,
by
=
c
(
"Name"
,
"Year"
))
# Create plot tables ----
pc_totals_plot
<-
data.frame
(
per_capita_totals
[
c
(
1
:
5
,
7
:
9
,
14
,
15
)])
pc_totals_plot
<-
data.frame
(
per_capita_totals
[
c
(
1
:
5
,
7
:
9
,
14
,
15
)])
pc_totals_plot
<-
melt
(
pc_totals_plot
,
id.vars
=
c
(
"CTRY18NM.RGN18NM"
,
"Second.Tier.Authority"
,
"Name"
,
"Code"
,
"Year"
))
pc_totals_plot
<-
melt
(
pc_totals_plot
,
id.vars
=
c
(
"CTRY18NM.RGN18NM"
,
"Second.Tier.Authority"
,
"Name"
,
"Code"
,
"Year"
))
pc_detail_plot
<-
data.frame
(
per_capita_detail
[
c
(
1
:
5
,
7
:
25
)])
pc_detail_plot
<-
melt
(
pc_detail_plot
,
id.vars
=
c
(
"CTRY18NM.RGN18NM"
,
"Second.Tier.Authority"
,
"Name"
,
"Code"
,
"Year"
))
pc_general_plot
<-
data.frame
(
per_capita_totals
[
c
(
3
,
5
,
11
:
13
)])
## Subset data - Southampton by default
## Subset data - Southampton by default
auth_area
<-
"New Forest"
auth_area
<-
"New Forest"
...
@@ -121,6 +129,12 @@ ghg_subset <- function(dt, auth_area = "Southampton"){
...
@@ -121,6 +129,12 @@ ghg_subset <- function(dt, auth_area = "Southampton"){
}
}
# Construct plots ----
# Construct plots ----
i
<-
1
plotNames
<-
list
()
for
(
auth_area
in
las_to_load
)
{
plotName
<-
paste0
(
"laemissions_by_year_"
,
gsub
(
"[[:space:]]"
,
"_"
,
auth_area
))
plotCaption
=
paste0
(
"Emissions data: Department for Business, Energy & Industrial Strategy"
,
plotCaption
=
paste0
(
"Emissions data: Department for Business, Energy & Industrial Strategy"
,
"\nUK local authority and regional carbon dioxide emissions national statistics: 2005 to 2018"
,
"\nUK local authority and regional carbon dioxide emissions national statistics: 2005 to 2018"
,
...
@@ -148,12 +162,25 @@ ggplot() +
...
@@ -148,12 +162,25 @@ ggplot() +
theme
(
legend.position
=
"right"
)
+
theme
(
legend.position
=
"right"
)
+
theme_classic
()
theme_classic
()
plotNames
[
i
]
<-
plotName
ggsave
(
paste0
(
here
::
here
(),
"/howTo/openGeogAPI/plots/"
,
plotName
,
".png"
),
dpi
=
150
,
width
=
12
,
height
=
6
,
units
=
"in"
)
i
<-
i
+1
}
print
(
plotNames
)
plotly
::
ggplotly
(
plot
)
plotly
::
ggplotly
(
plot
)
# By local authority
# By local authority
years_to_plot
<-
c
(
2005
,
2018
)
for
(
plot_year
in
years_to_plot
)
{
plotName
<-
paste0
(
"laemissions_by_auth_"
,
gsub
(
"[[:space:]]"
,
"_"
,
plot_year
))
plot_year
<-
2005
plot_data1
<-
pc_detail_plot
%>%
filter
(
Year
==
plot_year
)
plot_data1
<-
pc_detail_plot
%>%
filter
(
Year
==
plot_year
)
plot_data2
<-
pc_totals_plot
%>%
filter
(
Year
==
plot_year
)
plot_data2
<-
pc_totals_plot
%>%
filter
(
Year
==
plot_year
)
...
@@ -161,15 +188,14 @@ plot_data2 <- pc_totals_plot %>% filter(Year == plot_year)
...
@@ -161,15 +188,14 @@ plot_data2 <- pc_totals_plot %>% filter(Year == plot_year)
plotTitle
=
paste0
(
"Greenhouse gas emissions by sector for Hampshire local authorities: "
,
plot_year
)
plotTitle
=
paste0
(
"Greenhouse gas emissions by sector for Hampshire local authorities: "
,
plot_year
)
ggplot
()
+
ggplot
()
+
geom_col
(
data
=
plot_data1
,
aes
(
x
=
Name
,
y
=
value
,
fill
=
variable
),
position
=
"stack"
)
+
geom_col
(
data
=
plot_data1
,
aes
(
x
=
reorder
(
Name
,
desc
(
Name
))
,
y
=
value
,
fill
=
variable
),
position
=
"stack"
)
+
geom_col
(
data
=
plot_data2
,
aes
(
x
=
Name
,
y
=
value
,
colour
=
variable
),
fill
=
"none"
,
position
=
"stack"
)
+
geom_col
(
data
=
plot_data2
,
aes
(
x
=
reorder
(
Name
,
desc
(
Name
))
,
y
=
value
,
colour
=
variable
),
fill
=
"none"
,
position
=
"stack"
)
+
geom_hline
(
yintercept
=
0
,
lwd
=
0.4
,
colour
=
"black"
,
linetype
=
"dashed"
)
+
geom_hline
(
yintercept
=
0
,
lwd
=
0.4
,
colour
=
"black"
,
linetype
=
"dashed"
)
+
coord_
cartesian
(
x
lim
=
c
(
-1
,
8
))
+
coord_
flip
(
y
lim
=
c
(
-1
,
15
))
+
#scale_y_continuous(labels=abs) +
#scale_y_continuous(labels=abs) +
#scale_x_continuous(breaks = 2005:2018) +
#scale_x_continuous(breaks = 2005:2018) +
scale_color_manual
(
values
=
totals_pal
,
guide
=
FALSE
)
+
scale_color_manual
(
values
=
totals_pal
,
guide
=
FALSE
)
+
scale_fill_manual
(
values
=
detailed_pal
)
+
scale_fill_manual
(
values
=
detailed_pal
)
+
coord_flip
()
+
labs
(
x
=
"Local authority"
,
labs
(
x
=
"Local authority"
,
y
=
"Emissions per capita, tCO2"
,
y
=
"Emissions per capita, tCO2"
,
fill
=
"Source"
,
fill
=
"Source"
,
...
@@ -178,6 +204,10 @@ ggplot() +
...
@@ -178,6 +204,10 @@ ggplot() +
theme
(
legend.position
=
"none"
)
+
theme
(
legend.position
=
"none"
)
+
theme_classic
()
theme_classic
()
ggsave
(
paste0
(
here
::
here
(),
"/howTo/openGeogAPI/plots/"
,
plotName
,
".png"
),
dpi
=
150
,
width
=
12
,
height
=
6
,
units
=
"in"
)
}
ggplot
()
+
ggplot
()
+
geom_col
(
data
=
plot_data1
,
aes
(
x
=
Name
,
y
=
value
,
fill
=
variable
),
position
=
"stack"
)
+
geom_col
(
data
=
plot_data1
,
aes
(
x
=
Name
,
y
=
value
,
fill
=
variable
),
position
=
"stack"
)
+
geom_col
(
data
=
plot_data2
,
aes
(
x
=
Name
,
y
=
value
,
colour
=
variable
),
fill
=
"none"
,
position
=
"stack"
)
+
geom_col
(
data
=
plot_data2
,
aes
(
x
=
Name
,
y
=
value
,
colour
=
variable
),
fill
=
"none"
,
position
=
"stack"
)
+
...
...
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