Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenuial committed May 1, 2024
1 parent e29a55b commit 140984b
Show file tree
Hide file tree
Showing 36 changed files with 467 additions and 332 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ Imports:
magrittr,
methods,
purrr,
preferably,
rlang,
scales,
showtext,
stringr,
sysfonts,
utils,
wesanderson,
preferably
wesanderson
Remotes:
Nenuial/geotools
Depends:
Expand Down
4 changes: 4 additions & 0 deletions R/fonts.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' @param system_wide Install the fonts for all users?
#'
#' @return Move fonts to MacOS font folder
#' @keywords internal
#' @export
#'
ggeo_install_fonts_macos <- function(system_wide = FALSE) {
new_font_path <- "~/Library/Fonts/"
if (system_wide) new_font_path <- "/Library/Fonts/"
Expand All @@ -22,6 +24,8 @@ ggeo_install_fonts_macos <- function(system_wide = FALSE) {
#'
#' @return Move fonts to Linux font folders
#' @export
#' @keywords internal
#'
ggeo_install_fonts_linux <- function() {
fs::dir_ls(path = ggeo_file("fonts/")) |>
purrr::walk(
Expand Down
78 changes: 78 additions & 0 deletions R/ggplot_scales.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Format labels
#'
#' These functions extend the {[scales](https://scales.r-lib.org)}
#' package and allow formatting labels.
#'
#' @details
#' [`ggeo_label_sci_10()`] is used to format numbers with a clean
#' scientific format using a multiplier and not the *ugly* notation
#' using the letter *e*.
#'
#' [`ggeo_label_pyramid()`] is used for population pyramids with
#' absolute numbers. It formats the absolute number using
#' [`ggeo_label_sci_10()`].
#'
#' [`ggeo_label_abs_percent()`] is also used for population pyramids
#' but with relative numbers (percents). It uses [`scales::percent()`]
#' and absolute numbers.
#'
#' @param x Number to format
#'
#' @return A formatted string for the scales
#' @export
ggeo_label_sci_10 <- function(x) {
parse(text = gsub("e\\+?", "%.%10^", scales::scientific_format()(x)))
}

#' @rdname ggeo_label_sci_10
#' @export
ggeo_label_pyramid <- function(x) {
ggeo_label_sci_10(abs(x))
}

#' @rdname ggeo_label_sci_10
#' @export
ggeo_label_abs_percent <- function(x) {
scales::percent(abs(x), accuracy = 0.1)
}

#' Format population labels on pyramids
#'
#' `r lifecycle::badge("deprecated")`
#'
#' @param x The label
#' @param ... For compatibility
#'
#' @return Absolute
#' @export
#' @keywords internal
ggeoformat_pyramid_pop <- function(x, ...) {
scales::number(abs(x), scale = 1e-3)
}

#' Remove specific breaks
#'
#' This function can be used to modify the breaks of a ggplot2
#' scale. It is specifically designed to remove the some breaks
#' in the scale.
#'
#' @param original_func The function to create the breaks.
#' Use the break functions from the {[scales](https://scales.r-lib.org)}
#' @param remove_list The values to remove from the scale.
#'
#' @return A list
#' @export
#' @examples
#' ggplot2::ggplot(ggplot2::aes(x = speed, y = dist), data = cars) +
#' ggplot2::geom_point() +
#' ggplot2::labs(title = "Fast cars!") +
#' ggplot2::scale_y_continuous(
#' breaks = ggeo::ggeo_remove_breaks(scales::breaks_pretty(6), list(0)),
#' )
#'
ggeo_remove_breaks <- function(original_func, remove_list = list()) {
function(x) {
original_result <- original_func(x)
original_result[!(original_result %in% remove_list)]
}
}
66 changes: 60 additions & 6 deletions R/plot_save.R → R/ggplot_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,26 @@ ggeosave <- function(filename, ...,

#' Save function
#'
#' @param plot The ggplot2 object to save (should by piped in ;)
#' @param plot The ggplot2 object to save (should be piped in ;)
#' @param filename Path for filename (with extension!)
#' @param width Width. Defaults to keynote width (full = geotools::gtl_options("plot_full_width"))
#' @param height Height. Defaults to keynote height (full = geotools::gtl_options("plot_full_width"))
#' @param dpi DPI. Defaults to 72.
#' @param units Units. Defaults to cm.
#' @param ... Arguments to pass to ggplot2::ggsave
#' @param width The plot width.
#' Defaults to keynote width (`geotools::gtl_options("plot_standard_width")`).
#' For full slide width use `geotools::gtl_options("plot_full_width")`.
#' @param height The plot height.
#' Defaults to keynote height (`geotools::gtl_options("plot_standard_height")`).
#' For full slide height use `geotools::gtl_options("plot_full_height")`.
#' @param dpi The DPI. Default is 72.
#' @param units Units. Default is cm.
#' @inheritDotParams ggplot2::ggsave device scale limitsize bg create.dir
#'
#' @export
#' @examplesIf interactive()
#' cars |>
#' ggplot2::ggplot(ggplot2::aes(x = speed, y = dist)) +
#' ggplot2::geom_point() -> simple_plot
#'
#' ggeo_save(simple_plot, "simple_plot.png")
#'
ggeo_save <- function(plot,
filename,
width = geotools::gtl_options("plot_standard_width"),
Expand All @@ -75,3 +86,46 @@ ggeo_save <- function(plot,
...
)
}


#' Copitalize plot title
#'
#' @param plot A ggplot2 object
#'
#' @return A ggplot2 object
#' @export
#' @examplesIf interactive()
#' ggplot(aes(x = speed, y = dist), data = cars) +
#' geom_point() +
#' labs(title = "Fast cars !") +
#' ggeo_remove_title()
#'
ggeo_capitalize_title <- function(plot) {
plot$labels$title %>%
stringr::str_to_upper() %>%
stringr::str_replace_all("COLOR:#", "color:#") -> plot$labels$title

plot$patches$annotation$title %>%
stringr::str_to_upper() %>%
stringr::str_replace("COLOR:#", "color:#") -> plot$patches$annotation$title

plot
}

#' Remove plot title
#'
#' @param plot A ggplot2 object
#'
#' @return A ggplot2 object
#' @export
#' @examplesIf interactive()
#' ggplot(aes(x = speed, y = dist), data = cars) +
#' geom_point() +
#' labs(title = "Fast cars !") +
#' ggeo_remove_title()
#'
ggeo_remove_title <- function(plot) {
plot$labels$title <- NULL
plot$patches$annotation$title <- NULL
plot
}
13 changes: 8 additions & 5 deletions R/gis.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
#'
#' @seealso [geotools::gtl_crs_proj()]
#'
#' @param proj A string for the projection code
#' @inheritParams geotools::gtl_crs_proj
#' @inheritDotParams ggplot2::coord_sf
#'
#' @return A ggplot2 coord object
#' @export
#' @examplesIf interactive()
#' ggeo_coord("eqearth")
#' @examples
#' rnaturalearth::ne_countries() |>
#' ggplot2::ggplot() +
#' ggplot2::geom_sf() +
#' ggeo_coord("eqearth")
#'
ggeo_coord <- function(proj, ...) {
crs <- geotools::gtl_crs_proj(proj)
ggeo_coord <- function(code, ...) {
crs <- geotools::gtl_crs_proj(code)

ggplot2::coord_sf(crs = crs, datum = NA, ...)
}
43 changes: 27 additions & 16 deletions R/highcharter_themes.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
#' Highchart Theme: Purple
#' Add a purple theme to a highchart
#' Highcharts themes
#'
#' @param hc A highchart object
#' These functions can be used to modify the theme of
#' a highcharts plot.
#'
#' @return A highchart object
#' @param hc A highcharts object
#'
#' @return A highcharts object
#' @export
#' @examples
#' highcharter::hchart(
#' cars, "point",
#' highcharter::hcaes(x = speed, y = dist)
#' ) |>
#' hc_purple_theme()
#'
#' highcharter::hchart(
#' cars, "point",
#' highcharter::hcaes(x = speed, y = dist)
#' ) |>
#' hc_samarqand_theme()
#'
#' highcharter::hchart(
#' cars, "point",
#' highcharter::hcaes(x = speed, y = dist)
#' ) |>
#' hc_web_theme()
#'
hc_purple_theme <- function(hc) {
base_colors <- list(
main_color = "#e0d9fb",
Expand Down Expand Up @@ -86,12 +107,7 @@ hc_purple_theme <- function(hc) {
highcharter::hc_add_theme(theme)
}

#' Highchart Theme: Samarqand
#' Add a samarqand theme to a highchart
#'
#' @param hc A highchart object
#'
#' @return A highchart object
#' @rdname hc_purple_theme
#' @export
hc_samarqand_theme <- function(hc) {
base_colors <- list(
Expand Down Expand Up @@ -174,12 +190,7 @@ hc_samarqand_theme <- function(hc) {
highcharter::hc_add_theme(theme)
}

#' Highchart Theme: Web
#' Add a web theme to a highchart
#'
#' @param hc A highchart object
#'
#' @return A highchart object
#' @rdname hc_purple_theme
#' @export
hc_web_theme <- function(hc) {
base_colors <- list(
Expand Down
41 changes: 0 additions & 41 deletions R/plot_utils.R

This file was deleted.

59 changes: 0 additions & 59 deletions R/scales.R

This file was deleted.

1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @return A file path
#'
#' @keywords internal
#' @export
ggeo_file = function(...) {
system.file(..., package = 'ggeo', mustWork = TRUE)
Expand Down
Loading

0 comments on commit 140984b

Please sign in to comment.