#' Plot WHO
#'
#' Create plots based on the WHO data
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_who <- function(data, fig = NULL){
theme_who <- function(){
ggplot2::theme_bw() +
theme(axis.text.x = element_text(angle = 90,
hjust = 0,
vjust = 0.5))
}
# Handle figure specification
if(is.null(fig)){
fig <- names(data)[1]
}
# Extract the sub data
sub_data <- data[[fig]]
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile))-1)
cols <- c(cols, 'darkgrey')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
fill = quintile)) +
geom_bar(stat = 'identity',
position = position_dodge(width = 0.8)) +
scale_fill_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
#' Plot Figure 1
#'
#' Create plots based on the WHO data figure 1
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_fig1 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$fig1
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile))-1)
cols <- c(cols, 'darkgrey')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
fill = quintile)) +
geom_bar(stat = 'identity',
position = position_dodge(width = 0.8)) +
scale_fill_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
#' Plot Figure 2
#'
#' Create plots based on the WHO data figure 2
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_fig2 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$fig2
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile))-1)
cols <- c(cols, 'darkgrey')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
fill = quintile)) +
facet_wrap(~grp, scales = 'free_y') +
geom_bar(stat = 'identity',
position = position_dodge(width = 0.8)) +
scale_fill_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
#' Plot Figure 3
#'
#' Create plots based on the WHO data figure 3
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_fig3 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$fig3
# Define colors
# cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$grp)))
cols <- RColorBrewer::brewer.pal(n = length(unique(sub_data$grp)), 'Set2')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = grp,
color = grp)) +
geom_line(size = 3, alpha =0.7) +
scale_color_manual(name = '',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
#' Plot Figure 4
#'
#' Create plots based on the WHO data figure 4
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_fig4 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$fig4
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile)))
# cols <- c(cols, 'darkgrey')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
fill = quintile)) +
geom_bar(stat = 'identity',
position = position_dodge(width = 0.8),
color = 'black',
size = 0.3) +
scale_fill_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom') +
geom_text(aes(label = round(value, digits = 2)),
position = position_dodge(width = 0.8),
size = 3,
vjust = 0)
g
}
#' Plot Figure 5
#'
#' Create plots based on the WHO data figure 5
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_fig5 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$fig5
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile))-1)
cols <- c(cols, 'black')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
color = quintile,
fill = quintile)) +
facet_wrap(~grp, scales = 'free_y') +
geom_line() +
# geom_bar(stat = 'identity',
# position = position_dodge(width = 0.8)) +
scale_color_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
#' Plot T2
#'
#' Create plots based on the WHO data T2
#' @param data A data list, as generated by read_data
#' @return a ggplot2 object
#' @noRd
#' @import ggplot2
#' @import dplyr
#' @export
plot_t2 <- function(data){
theme_who <- ggplot2::theme_bw
# Extract the sub data
sub_data <- data$t2c
# Define colors
cols <- colorRampPalette(c('red', 'yellow', 'darkgreen'))(length(unique(sub_data$quintile))-1)
cols <- c(cols, 'black')
# Prepare the base plot
g <- ggplot(data = sub_data,
aes(x = year,
y = value,
group = quintile,
color = quintile,
fill = quintile)) +
facet_wrap(~grp, scales = 'free_y') +
geom_line() +
# geom_bar(stat = 'identity',
# position = position_dodge(width = 0.8)) +
scale_color_manual(name = 'Quintile',
values = cols) +
theme_who() +
labs(x = 'Year',
y = 'Value',
subtitle = sub_data$indicator[1]) +
theme(legend.position = 'bottom')
g
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.