Skip to content

Commit

Permalink
Merge pull request #75 from selkamand/74-add-options-to-disable-prett…
Browse files Browse the repository at this point in the history
…ification-of-axis-text

74 add options to disable prettification of axis text
  • Loading branch information
selkamand authored Nov 20, 2024
2 parents a25e674 + 58ebf7a commit 643a558
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
URL: https://github.com/selkamand/gg1d, https://selkamand.github.io/gg1d/
BugReports: https://github.com/selkamand/gg1d/issues
Imports:
Expand Down
41 changes: 31 additions & 10 deletions R/gg1d.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ choose_colours <- function(data, palettes, plottable, ndistinct, coltype, colour
#' @param legend_nrow the number of rows in the legend (number)
#' @param legend_ncol the number of columns in the legend. Set `legend_nrow = NULL` when using legend_ncol (number)
#' @param legend_title_size the size of the title of the legend (number)
#' @param legend_title_beautify beautify legend title (add spaces to snake_case / camelCase & capitalise each word) (flag)
#' @param beautify_text beautify y axis text and legend titles (add spaces to snake_case / camelCase & capitalise each word) (flag)
#' @param legend_text_size the size of the text in the legend (number)
#' @param legend_key_size the size of the key in the legend (number)
#' @param palettes A list of named vectors. List names correspond to \strong{data} column names (categorical only). Vector names to levels of columns. Vector values are colours, the vector names are used to map values in data to a colour.
Expand Down Expand Up @@ -224,7 +224,7 @@ gg1d <- function(
ignore_column_regex = "_ignore$",
show_legend_titles = FALSE, show_legend = !interactive, legend_position = c("right", "left", "bottom", "top"),
legend_title_position = c("top", "bottom", "left", "right"),
legend_title_beautify = TRUE,
beautify_text = TRUE,
numeric_plot_type = c('bar', "heatmap"),
legend_nrow = 4, legend_ncol = NULL,
legend_title_size = NULL, legend_text_size = NULL, legend_key_size = 0.3,
Expand Down Expand Up @@ -262,7 +262,7 @@ gg1d <- function(
assertions::assert_equal(length(colours_default_logical), 2)
assertions::assert_names_include(colours_default_logical, c("TRUE", "FALSE"))
assertions::assert_string(colours_missing)
assertions::assert_flag(legend_title_beautify)
assertions::assert_flag(beautify_text)
assertions::assert_number(vertical_spacing)
assertions::assert_string(ignore_column_regex)
assertions::assert_string(colours_heatmap_low)
Expand Down Expand Up @@ -437,7 +437,7 @@ gg1d <- function(
data,
aes(
x = .data[[col_id]],
y = if(legend_title_beautify) beautify(colname) else colname,
y = if(beautify_text) beautify(colname) else colname,
fill = .data[[colname]]
)
) +
Expand All @@ -448,7 +448,7 @@ gg1d <- function(
aes(label = na_marker), size = na_marker_size, na.rm = TRUE, vjust=0.5, color = na_marker_colour,
) }} +
ggplot2::scale_x_discrete(drop = drop_unused_id_levels) +
#ggplot2::ylab(if(legend_title_beautify) beautify(colname) else colname) +
#ggplot2::ylab(if(beautify_text) beautify(colname) else colname) +
theme_categorical(
show_legend_titles = show_legend_titles,
show_legend = show_legend,
Expand All @@ -461,7 +461,7 @@ gg1d <- function(
) +
ggplot2::guides(fill = ggplot2::guide_legend(
title.position = legend_title_position,
title = if(legend_title_beautify) beautify(colname) else colname,
title = if(beautify_text) beautify(colname) else colname,
nrow = legend_nrow,
ncol = legend_ncol
)) +
Expand All @@ -473,7 +473,7 @@ gg1d <- function(
breaks <- sensible_3_breaks(data[[colname]])
labels <- sensible_3_labels(
data[[colname]],
axis_label = if(legend_title_beautify) beautify(colname) else colname,
axis_label = if(beautify_text) beautify(colname) else colname,
fontsize_numbers = fontsize_barplot_y_numbers
)

Expand All @@ -491,14 +491,14 @@ gg1d <- function(
position = y_axis_position,
expand = c(0,0)
) +
#ggplot2::ylab(if(legend_title_beautify) beautify(colname) else colname) +
#ggplot2::ylab(if(beautify_text) beautify(colname) else colname) +
theme_numeric_bar(vertical_spacing = vertical_spacing, fontsize_y_text = fontsize_y_text)
}
# Numeric Heatmap -------------------------------------------------------------------------
else if (coltype == "numeric" && numeric_plot_type == "heatmap") {
gg <- ggplot2::ggplot(data, aes(
x = .data[[col_id]],
y = if(legend_title_beautify) beautify(colname) else colname,
y = if(beautify_text) beautify(colname) else colname,
fill = .data[[colname]])
) +
ggiraph::geom_tile_interactive(mapping = aes_interactive, width = width, na.rm = TRUE) +
Expand Down Expand Up @@ -683,10 +683,11 @@ sensible_3_labels <- function(vector, axis_label, fontsize_numbers = 7){
#' Takes an input string and 'beautify' by converting underscores to spaces and
#'
#' @param string input string
#' @param autodetect_units automatically detect units (e.g. mm, kg, etc) and wrap in brackets.
#'
#' @return string
#'
beautify <- function(string){
beautify <- function(string, autodetect_units = TRUE){
# underscores to spaces
string <- gsub(x=string, pattern = "_", replacement = " ")

Expand All @@ -696,7 +697,27 @@ beautify <- function(string){
# camelCase to camel Case
string <- gsub(x=string, pattern = "([a-z])([A-Z])", replacement = "\\1 \\2")

# Autodetect units (and move to brackets)
if(autodetect_units){
string <- sub("\\bm\\b", "(m)", string)
string <- sub("\\bmm\\b", "(mm)", string)
string <- sub("\\cm\\b", "(cm)", string)
string <- sub("\\km\\b", "(km)", string)
string <- sub("\\bg\\b", "(g)", string)
string <- sub("\\bkg\\b", "(kg)", string)
string <- sub("\\bmg\\b", "(mm)", string)
string <- sub("\\boz\\b", "(oz)", string)
string <- sub("\\blb\\b", "(lb)", string)
string <- sub("\\bin\\b", "(in)", string)
string <- sub("\\bft\\b", "(ft)", string)
string <- sub("\\byd\\b", "(yd)", string)
string <- sub("\\bmi\\b", "(mi)", string)
}


# Capitalise Each Word
string <- gsub(x=string, pattern = "^([a-z])", perl = TRUE, replacement = ("\\U\\1"))
string <- gsub(x=string, pattern = " ([a-z])", perl = TRUE, replacement = (" \\U\\1"))

return(string)
}
4 changes: 3 additions & 1 deletion man/beautify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/gg1d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-beautify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("beautify works as expected", {
# Names are original values, values, are what beautify should them into
examples <- c(
"species" = "Species",
"island" = "Island",
"bill_length_mm" = "Bill Length (mm)",
"bill_depth_mm" = "Bill Depth (mm)",
"flipper_length_mm" = "Flipper Length (mm)",
"body_mass_g" = "Body Mass (g)",
"kgtest_kg" = "Kgtest (kg)",
"sex" = "Sex"
)

expect_equal(examples, beautify(names(examples)), ignore_attr = TRUE)
})

0 comments on commit 643a558

Please sign in to comment.