Just for backup / change of laptop
This commit is contained in:
parent
91734ed7f6
commit
c7cb7c0975
4 changed files with 119 additions and 1 deletions
|
|
@ -29,5 +29,7 @@ Imports:
|
||||||
tidyr,
|
tidyr,
|
||||||
dplyr,
|
dplyr,
|
||||||
ggalluvial
|
ggalluvial
|
||||||
|
Remotes:
|
||||||
|
github::hrbrmstr/waffle
|
||||||
Suggests: knitr, sf, tmap
|
Suggests: knitr, sf, tmap
|
||||||
VignetteBuilder: knitr
|
VignetteBuilder: knitr
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,8 @@ theme_reach <- function(
|
||||||
text_color = cols_reach("main_grey"),
|
text_color = cols_reach("main_grey"),
|
||||||
text_font_face = "plain",
|
text_font_face = "plain",
|
||||||
panel_background_color = "#FFFFFF",
|
panel_background_color = "#FFFFFF",
|
||||||
|
panel_border = FALSE,
|
||||||
|
panel_border_color = cols_reach("main_grey"),
|
||||||
legend_position = "right",
|
legend_position = "right",
|
||||||
legend_direction = "vertical",
|
legend_direction = "vertical",
|
||||||
legend_reverse = TRUE,
|
legend_reverse = TRUE,
|
||||||
|
|
@ -244,6 +246,14 @@ theme_reach <- function(
|
||||||
color = grid_minor_color,
|
color = grid_minor_color,
|
||||||
linewidth = grid_minor_y_size)
|
linewidth = grid_minor_y_size)
|
||||||
)
|
)
|
||||||
|
if (!panel_border) theme_reach <- theme_reach +
|
||||||
|
ggplot2::theme(
|
||||||
|
panel.border = ggplot2::element_blank()
|
||||||
|
) else theme_reach <- theme_reach +
|
||||||
|
ggplot2::theme(
|
||||||
|
panel.border = ggplot2::element_rect(color = panel_background_color)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Other parameters
|
# Other parameters
|
||||||
theme_reach <- theme_reach + ggplot2::theme(...)
|
theme_reach <- theme_reach + ggplot2::theme(...)
|
||||||
|
|
|
||||||
95
R/waffle.R
Normal file
95
R/waffle.R
Normal file
|
|
@ -0,0 +1,95 @@
|
||||||
|
#' @title Simple waffle chart
|
||||||
|
#'
|
||||||
|
#' @param df A data frame.
|
||||||
|
#' @param x A character column or coercible as a character column. Will give the waffle's fill color.
|
||||||
|
#' @param y A numeric column.
|
||||||
|
#' @param alpha Fill transparency.
|
||||||
|
#' @param title Plot title. Default to NULL.
|
||||||
|
#' @param subtitle Plot subtitle. Default to NULL.
|
||||||
|
#' @param caption Plot caption. Default to NULL.
|
||||||
|
#' @param arrange TRUE or FALSE. Arrange by highest percentage first.
|
||||||
|
#' @param theme Whatever theme. Default to theme_reach().
|
||||||
|
#'
|
||||||
|
#' @return A waffle chart to be used parsimoniously
|
||||||
|
#'
|
||||||
|
#' @export
|
||||||
|
waffle <- function(df,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
alpha = 1,
|
||||||
|
x_title = NULL,
|
||||||
|
title = NULL,
|
||||||
|
subtitle = NULL,
|
||||||
|
caption = NULL,
|
||||||
|
arrange = TRUE,
|
||||||
|
n_rows = 10,
|
||||||
|
size = 0.33,
|
||||||
|
colour = "white",
|
||||||
|
flip = FALSE,
|
||||||
|
theme = theme_reach(
|
||||||
|
|
||||||
|
axis_x = FALSE,
|
||||||
|
axis_y = FALSE)){
|
||||||
|
|
||||||
|
# Arrange by biggest prop first ?
|
||||||
|
if (arrange) df <- dplyr::arrange(
|
||||||
|
df,
|
||||||
|
dplyr::desc({{ y }})
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get levels for scaling
|
||||||
|
lev <- dplyr::pull(df, {{ x }})
|
||||||
|
df <- dplyr::mutate(df, "{{x}}" := factor({{ x }}, levels = lev))
|
||||||
|
|
||||||
|
# Mapping
|
||||||
|
g <- ggplot2::ggplot(
|
||||||
|
df,
|
||||||
|
mapping = ggplot2::aes(
|
||||||
|
fill = {{ x }},
|
||||||
|
color = {{ x }},
|
||||||
|
values = {{ y }}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add waffle geom
|
||||||
|
g <- g +
|
||||||
|
waffle::geom_waffle(
|
||||||
|
n_rows = n_rows,
|
||||||
|
size = size,
|
||||||
|
color = "white",
|
||||||
|
flip = flip,
|
||||||
|
alpha = alpha) +
|
||||||
|
ggplot2::coord_equal()
|
||||||
|
|
||||||
|
# Add title, subtitle, caption, x_title, y_title
|
||||||
|
g <- g + ggplot2::labs(
|
||||||
|
title = title,
|
||||||
|
subtitle = subtitle,
|
||||||
|
caption = caption,
|
||||||
|
fill = x_title,
|
||||||
|
color = x_title
|
||||||
|
)
|
||||||
|
|
||||||
|
# # No axis
|
||||||
|
# g <- g + ggplot2::theme(
|
||||||
|
# axis.line.x = ggplot2::element_blank(),
|
||||||
|
# axis.ticks.x = ggplot2::element_blank(),
|
||||||
|
# axis.text.x = ggplot2::element_blank(),
|
||||||
|
# axis.title.x = ggplot2::element_blank(),
|
||||||
|
# axis.line.y = ggplot2::element_blank(),
|
||||||
|
# axis.ticks.y = ggplot2::element_blank(),
|
||||||
|
# axis.text.y = ggplot2::element_blank(),
|
||||||
|
# axis.title.y = ggplot2::element_blank()
|
||||||
|
# )
|
||||||
|
|
||||||
|
# Basic theme
|
||||||
|
g <- g +
|
||||||
|
hrbrthemes::theme_ipsum() +
|
||||||
|
waffle::theme_enhance_waffle()
|
||||||
|
|
||||||
|
# Add theme
|
||||||
|
g <- g + theme
|
||||||
|
|
||||||
|
return(g)
|
||||||
|
|
||||||
|
}
|
||||||
13
README.Rmd
13
README.Rmd
|
|
@ -148,7 +148,7 @@ dumbbell(df,
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example 4: donut chart, REACH themed (to used moderately)
|
### Example 4: donut chart, REACH themed (to used once, not twice)
|
||||||
```{r example-donut-plot, out.width = "65%", warning = FALSE}
|
```{r example-donut-plot, out.width = "65%", warning = FALSE}
|
||||||
|
|
||||||
# Some summarized data: % of HHs by displacement status
|
# Some summarized data: % of HHs by displacement status
|
||||||
|
|
@ -170,6 +170,17 @@ donut(df,
|
||||||
theme = theme_reach(legend_reverse = TRUE))
|
theme = theme_reach(legend_reverse = TRUE))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Example 4: donut chart, REACH themed (to used once, not twice)
|
||||||
|
```{r example-donut-plot, out.width = "65%", warning = FALSE}
|
||||||
|
|
||||||
|
# Donut
|
||||||
|
waffle(df,
|
||||||
|
status,
|
||||||
|
percentage,
|
||||||
|
title = "% of HHs by displacement status",
|
||||||
|
theme = theme_reach(legend_reverse = TRUE))
|
||||||
|
```
|
||||||
|
|
||||||
### Example 5: alluvial chart, REACH themed
|
### Example 5: alluvial chart, REACH themed
|
||||||
```{r example-alluvial-plot, out.width = "65%", warning = FALSE}
|
```{r example-alluvial-plot, out.width = "65%", warning = FALSE}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue