Skip to content

Commit

Permalink
Update Figures
Browse files Browse the repository at this point in the history
  • Loading branch information
jschoeley committed Dec 5, 2020
1 parent b0d7984 commit f355470
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 137 deletions.
Binary file modified Figures/Figure_1.pdf
Binary file not shown.
Binary file modified Figures/Figure_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Figures/Figure_2.pdf
Binary file not shown.
Binary file modified Figures/Figure_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Figures/Figure_3.pdf
Binary file not shown.
Binary file modified Figures/Figure_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 14 additions & 9 deletions R/05_cumulative_excess_death_figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fig$figure1 <-
results$excess.deaths.week.sex %>%
filter(model == 'gam.nb') %>%
ggplot() +
geom_hline(yintercept = 0) +
geom_ribbon(
aes(
x = date,
Expand Down Expand Up @@ -63,7 +64,7 @@ fig$figure1 <-
filter(date == max(date))
) +
scale_x_date(
limits = as.Date(c('2020-03-01', '2020-12-01')),
limits = as.Date(c('2020-03-01', '2020-12-10')),
date_breaks = '1 month', date_labels = '%b'
) +
scale_y_continuous(labels = scales::label_comma()) +
Expand All @@ -73,7 +74,11 @@ fig$figure1 <-
x = NULL,
y = 'Cumulative excess deaths'
) +
fig_spec$MyGGplotTheme(hgrid = TRUE, vgrid = TRUE, scaler = 1.3, show_legend = FALSE)
coord_cartesian(expand = FALSE, clip = 'off') +
fig_spec$MyGGplotTheme(
grid = 'xy', scaler = 1.3, show_legend = FALSE,
axis = 'none'
)

fig_spec$ExportPDF(
fig$figure1, filename = 'Figure_1', path = 'Figures',
Expand All @@ -91,6 +96,7 @@ fig$figure2 <-
filter(model == 'gam.nb') %>%
mutate(age.n = factor(age.n, cnst$age.labels, names(cnst$age.labels))) %>%
ggplot() +
geom_hline(yintercept = 0) +
geom_ribbon(
aes(
x = date,
Expand All @@ -109,8 +115,8 @@ fig$figure2 <-
) +
facet_wrap(~age.n) +
scale_x_date(
limits = as.Date(c('2020-03-01', '2021-01-01')),
date_breaks = '2 months',
limits = as.Date(c('2020-03-01', '2020-12-31')),
date_breaks = '2 months', date_minor_breaks = '1 month',
date_labels = '%b'
) +
scale_y_continuous(labels = scales::label_comma()) +
Expand Down Expand Up @@ -149,12 +155,11 @@ fig$figure2 <-
group_by(sex, age.n) %>%
filter(date == max(date))
) +
coord_cartesian(expand = FALSE, clip = 'off') +
fig_spec$MyGGplotTheme(
hgrid = TRUE, vgrid = TRUE,
scaler = 1.3,
show_legend = FALSE
) +
coord_cartesian(clip = 'off')
grid = 'xy', minor_grid = 'x', scaler = 1.3, show_legend = FALSE,
axis = 'none'
)

fig_spec$ExportPDF(
fig$figure2, filename = 'Figure_2', path = 'Figures',
Expand Down
11 changes: 8 additions & 3 deletions R/08_plot_life_expectancy.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ fig$figure3 <-
aes(x = year, y = ex, color = sex),
size = 1
) +
#scale_y_continuous(labels = scales::label_comma()) +
scale_color_manual(values = c("#1E8B8F","#806152")) +
scale_fill_manual(values = c("#1E8B8F","#806152")) +
labs(
Expand All @@ -47,7 +46,10 @@ fig$figure3 <-
angle = c(30,38),
color = c("#1E8B8F","#806152"),
size = 6.5, hjust = 0, vjust = 1) +
fig_spec$MyGGplotTheme(hgrid = TRUE, scaler = 1.3, show_legend = FALSE)
fig_spec$MyGGplotTheme(
grid = 'xy', scaler = 1.3, show_legend = FALSE,
axis = 'x'
)

fig$figure4 <-
fig.data %>%
Expand Down Expand Up @@ -76,7 +78,10 @@ fig$figure4 <-
x = NULL,
y = NULL
) +
fig_spec$MyGGplotTheme(hgrid = TRUE, scaler = 1.3, show_legend = FALSE)
fig_spec$MyGGplotTheme(
grid = 'xy', scaler = 1.3, show_legend = FALSE,
axis = 'x'
)

library(patchwork)

Expand Down
283 changes: 158 additions & 125 deletions R/Figure_specifications.R
Original file line number Diff line number Diff line change
@@ -1,125 +1,158 @@
# figure specs
fig_spec <- list()

# ggplot theme ----------------------------------------------------

# ggplot theme by Jonas Schöley
fig_spec$MyGGplotTheme <-
function (
size = 8,
family = 'sans',
scaler = 1,
no_axes = FALSE,
panel_border = FALSE,
hgrid = FALSE,
vgrid = FALSE,
show_legend = TRUE,
ar = NA
) {

size_med = size*scaler
size_sml = round(size*0.7)*scaler
base_linesize = 0.3*scaler

list(
theme_classic(base_size = size_med, base_family = family),
theme(
# basic
text = element_text(color = 'black'),
line = element_line(size = base_linesize, lineend = 'square'),
# axis
#axis.line.y = element_blank(),
axis.title = element_text(size = size_med, face = 'bold'),
axis.ticks = element_line(size = rel(0.5), color = 'black'),
axis.text = element_text(size = size_med, color = 'black'),
# strips
strip.text = element_text(color = 'black', size = size_med),
strip.background = element_blank(),
# plot
title = element_text(face = 'bold'),
plot.subtitle = element_text(color = 'black', size = size_med, face = 'bold'),
plot.caption = element_text(color = 'black', size = size_sml, face = 'plain'),
plot.background = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(1, 0.1, 0.5, 0.5), units = 'mm'),
),
if (isTRUE(hgrid)) {
theme(panel.grid.major.y =
element_line(linetype = 3, color = 'grey80'))
},
if (isTRUE(vgrid)) {
theme(panel.grid.major.x =
element_line(linetype = 3, color = 'grey80'))
},
if (isTRUE(panel_border)) {
theme(
panel.border =
element_rect(fill = NA)
)
},
if (!isTRUE(show_legend)) {
theme(legend.position = 'none')
},
if (isTRUE(no_axes)) {
theme(
axis.line = element_blank()
)
},
if (!is.na(ar)) {
theme(
aspect.ratio = ar
)
}
)
}

# Dimensions ------------------------------------------------------

# figure width (mm)
fig_spec$width = 170

# Colors ----------------------------------------------------------

# color palette
fig_spec$discrete_colors <-
c('#D23737', # red
'#3191C9', # blue
'#D2BC2D', # yellow
'#4EC93B', # green
'#881F93', # purple
'#C5752B') # orange
fig_spec$discrete_colors_light <-
c('#FCB3B3', # red
'#A7DDFC', # blue
'#FAEC8E' # yellow
)

fig_spec$sex_colors <-
c(`m` = '#806152', `f` = '#1E8B8F')

# Export function -------------------------------------------------

fig_spec$ExportPDF <-
function (figure, filename, path, ...) {
ggsave(
filename = paste0(filename, '.pdf'),
plot = figure,
path = path,
units = 'mm',
dpi = 300,
useDingbats = FALSE,
...
)
}

fig_spec$ExportPNG <-
function (figure, filename, path, ...) {
ggsave(
filename = paste0(filename, '.png'),
plot = figure,
path = path,
units = 'mm',
dpi = 300,
...
)
}
# figure specs
fig_spec <- list()

# ggplot theme ----------------------------------------------------

# ggplot theme by Jonas Schöley
fig_spec$MyGGplotTheme <-
function (
size = 8,
family = 'sans',
scaler = 1,
axis = 'x',
panel_border = FALSE,
grid = 'y',
minor_grid = '',
show_legend = TRUE,
ar = NA
) {

size_med = size*scaler
size_sml = round(size*0.7)*scaler
base_linesize = 0.3*scaler

list(
theme_minimal(base_size = size_med, base_family = family),
theme(
# basic
text = element_text(color = 'black'),
line = element_line(size = base_linesize, lineend = 'square'),
# axis
#axis.line.y = element_blank(),
axis.title = element_text(size = size_med, face = 'bold'),
#axis.ticks = element_line(size = rel(0.5), color = 'black'),
axis.text = element_text(size = size_med, color = 'black'),
# strips
strip.text = element_text(color = 'black', size = size_med),
strip.background = element_blank(),
# plot
title = element_text(face = 'bold'),
plot.subtitle = element_text(color = 'black', size = size_med, face = 'bold'),
plot.caption = element_text(color = 'black', size = size_sml, face = 'plain'),
plot.background = element_blank(),
panel.background = element_blank(),
#plot.margin = unit(c(1, 0.1, 0.5, 0.5), units = 'mm'),
# grid
panel.grid = element_blank()
),
if (identical(grid, 'y')) {
theme(panel.grid.major.y =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (identical(grid, 'x')) {
theme(panel.grid.major.x =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (identical(grid, 'xy') | identical(grid, 'yx')) {
theme(panel.grid.major.y =
element_line(size = base_linesize, linetype = 3, color = 'grey80'),
panel.grid.major.x =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (identical(minor_grid, 'y')) {
theme(panel.grid.minor.y =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (identical(minor_grid, 'x')) {
theme(panel.grid.minor.x =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (identical(minor_grid, 'xy') | identical(grid, 'yx')) {
theme(panel.grid.minor.y =
element_line(size = base_linesize, linetype = 3, color = 'grey80'),
panel.grid.minor.x =
element_line(size = base_linesize, linetype = 3, color = 'grey80'))
},
if (isTRUE(panel_border)) {
theme(
panel.border =
element_rect(fill = NA)
)
},
if (!isTRUE(show_legend)) {
theme(legend.position = 'none')
},
if (axis == 'x') {
theme(
axis.line.x = element_line(linetype = 1, color = 'black')
)
},
if (axis == 'y') {
theme(
axis.line.y = element_line(linetype = 1, color = 'black')
)
},
if (axis == 'xy') {
theme(
axis.line = element_line(linetype = 1, color = 'black')
)
},
if (!is.na(ar)) {
theme(
aspect.ratio = ar
)
}
)
}


# Dimensions ------------------------------------------------------

# figure width (mm)
fig_spec$width = 170

# Colors ----------------------------------------------------------

# color palette
fig_spec$discrete_colors <-
c('#D23737', # red
'#3191C9', # blue
'#D2BC2D', # yellow
'#4EC93B', # green
'#881F93', # purple
'#C5752B') # orange
fig_spec$discrete_colors_light <-
c('#FCB3B3', # red
'#A7DDFC', # blue
'#FAEC8E' # yellow
)

fig_spec$sex_colors <-
c(`m` = '#806152', `f` = '#1E8B8F')

# Export function -------------------------------------------------

fig_spec$ExportPDF <-
function (figure, filename, path, ...) {
ggsave(
filename = paste0(filename, '.pdf'),
plot = figure,
path = path,
units = 'mm',
dpi = 300,
useDingbats = FALSE,
...
)
}

fig_spec$ExportPNG <-
function (figure, filename, path, ...) {
ggsave(
filename = paste0(filename, '.png'),
plot = figure,
path = path,
units = 'mm',
dpi = 300,
...
)
}

0 comments on commit f355470

Please sign in to comment.