-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqplot_histogram_density.R
63 lines (61 loc) · 2.47 KB
/
qplot_histogram_density.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
55
56
57
58
59
60
61
62
63
#' Title
#'
#' @param data_exposure_plot data_exposure_plot
#'
#' @export
#'
qplot_histogram_density <- function(data_exposure_plot) {
phist <-
ggplot2::ggplot(
data_exposure_plot,
ggplot2::aes(x = .data$value_to_plot, fill = .data$group_mean)
) +
ggplot2::geom_histogram(bins = 15) +
ggplot2::scale_y_continuous(
expand = ggplot2::expansion(mult = c(0, .1))
) +
ggplot2::scale_x_continuous(labels = scales::unit_format(unit = "M", scale = 1e-6)) +
ggplot2::scale_fill_gradient(
low = r2dii.colours::palette_1in1000_plot |> dplyr::filter(.data$label == "grey") |> dplyr::pull(.data$hex),
high = r2dii.colours::palette_1in1000_plot |> dplyr::filter(.data$label == "red") |> dplyr::pull(.data$hex),
name = "Average exposure\n per sector (millions)"
) +
r2dii.plot::theme_2dii() +
ggplot2::theme(
legend.title = ggplot2::element_text(),
axis.title.x = ggplot2::element_blank()
) +
ggplot2::labs(
title = "Distributions of exposures per sector",
subtitle = "Demonstrated using histograms and density plots.\n The colours represent the average size of the exposure within a data set.",
x = "Exposure (millions)",
y = "Number of companies"
) +
ggplot2::facet_wrap(~ .data$group_variable, scales = "free") +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))
pdens <-
ggplot2::ggplot(
data_exposure_plot,
ggplot2::aes(x = .data$value_to_plot, fill = .data$group_mean)
) +
ggplot2::geom_density(ggplot2::aes(y = ..scaled..)) +
ggplot2::scale_x_continuous(labels = scales::unit_format(unit = "M", scale = 1e-6)) +
ggplot2::scale_y_continuous(expand = ggplot2::expansion(mult = c(0, .1))) +
ggplot2::scale_fill_gradient(
low = r2dii.colours::palette_1in1000_plot |> dplyr::filter(.data$label == "grey") |> dplyr::pull(.data$hex),
high = r2dii.colours::palette_1in1000_plot |> dplyr::filter(.data$label == "red") |> dplyr::pull(.data$hex),
name = "Average exposure\n per sector (millions)"
) +
r2dii.plot::theme_2dii() +
ggplot2::theme(
legend.title = ggplot2::element_text(),
strip.text = ggplot2::element_blank()
) +
ggplot2::labs(
x = "Exposure (millions)",
y = "Density"
) +
ggplot2::facet_wrap(~ .data$group_variable, scales = "free") +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))
gridExtra::grid.arrange(phist, pdens, ncol = 1)
}