Skip to content

Commit

Permalink
complete examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jbengler committed Jul 12, 2024
1 parent 073709d commit 095e292
Show file tree
Hide file tree
Showing 13 changed files with 384 additions and 11 deletions.
73 changes: 73 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,24 @@ tidyplot <- function(data, ..., width = 50, height = 50, dodge_width = 0.8) {
#' @inherit common_arguments
#'
#' @examples
#' # Split by year
#' energy %>%
#' dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>%
#' tidyplot(y = power, color = energy_source) %>%
#' add_donut() %>%
#' split_plot(by = year)
#' # Change dimensions of subplots
#' energy %>%
#' dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>%
#' tidyplot(y = power, color = energy_source) %>%
#' add_donut() %>%
#' split_plot(by = year, widths = 15, heights = 15)
#' # Spread plots across multiple pages
#' energy %>%
#' dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>%
#' tidyplot(y = power, color = energy_source) %>%
#' add_donut() %>%
#' split_plot(by = year, ncol = 2, nrow = 1)
#'
#' @export
split_plot <- function(plot, by, ncol = NULL, nrow = NULL, byrow = NULL,
Expand Down Expand Up @@ -135,6 +148,31 @@ split_plot <- function(plot, by, ncol = NULL, nrow = NULL, byrow = NULL,
#' @param title Plot title.
#' @param ... Arguments passed on to `print()`.
#' @inherit common_arguments
#'
#' @details
#' * `view_plot()` supports data subsetting. See examples and [Advanced plotting](https://jbengler.github.io/tidyplots/articles/Advanced-plotting.html#data-subsetting).
#'
#' @examples
#' # View intermediate stages on screen
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar(alpha = 0.3) %>%
#' add_sem_bar() %>%
#' add_data_points_beeswarm() %>%
#' view_plot(title = "Before changing color scheme") %>%
#' adjust_colors(colors_discrete_seaside) %>%
#' view_plot(title = "After changing color scheme")
#' # View data subsets on screen
#' gene_expression %>%
#' tidyplot(x = condition, y = expression, color = sample_type) %>%
#' add_mean_dash() %>%
#' add_sem_bar() %>%
#' add_data_points_beeswarm() %>%
#' view_plot(data = filter_rows(external_gene_name == "Apol6"),
#' title = "Apol6") %>%
#' view_plot(data = filter_rows(external_gene_name == "Bsn"),
#' title = "Bsn")
#'
#' @export
view_plot <- function(plot, data = all_rows(), title = ggplot2::waiver(), ...) {
check_tidyplot(plot)
Expand Down Expand Up @@ -162,6 +200,41 @@ view_plot <- function(plot, data = all_rows(), title = ggplot2::waiver(), ...) {
#' @inheritParams ggplot2::ggsave
#' @inherit common_arguments
#'
#' @examples
#' if (FALSE) {
#'
#' # Save plot to file
#' study %>%
#' tidyplot(treatment, score) %>%
#' add_data_points() %>%
#' save_plot("single_plot.pdf")
#'
#' # Save multipage PDF file
#' gene_expression %>%
#' tidyplot(group, expression, color = sample_type) %>%
#' add_data_points() %>%
#' split_plot(by = external_gene_name, nrow = 3, ncol = 3) %>%
#' save_plot("multipage_plot.pdf")
#'
#' # Save multiple PDF files
#' gene_expression %>%
#' tidyplot(group, expression, color = sample_type) %>%
#' add_data_points() %>%
#' split_plot(by = external_gene_name, nrow = 3, ncol = 3) %>%
#' save_plot("plot.pdf", multiple_files = TRUE)
#'
#' # Save intermediate stages to file
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar(alpha = 0.3) %>%
#' add_sem_bar() %>%
#' add_data_points_beeswarm() %>%
#' save_plot("before.pdf") %>%
#' adjust_colors(colors_discrete_seaside) %>%
#' save_plot("after.pdf")
#'
#' }
#'
#' @export
save_plot <- function(plot = ggplot2::last_plot(), filename,
width = NA, height = NA, units = c("mm", "cm", "in"),
Expand Down
73 changes: 73 additions & 0 deletions R/remove.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@

#' Remove legend or legend title
#' @inherit common_arguments
#'
#' @examples
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_legend()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_legend_title()
#'
#' @export
remove_legend <- function(plot) {
check_tidyplot(plot)
Expand All @@ -16,6 +27,29 @@ remove_legend_title <- function(plot) {

#' Remove x axis or parts of it
#' @inherit common_arguments
#'
#' @examples
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_x_axis_line()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_x_axis_ticks()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_x_axis_labels()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_x_axis_title()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_x_axis()
#'
#' @export
remove_x_axis <- function(plot) {
check_tidyplot(plot)
Expand Down Expand Up @@ -57,6 +91,29 @@ remove_x_axis_title <- function(plot) {

#' Remove y axis or parts of it
#' @inherit common_arguments
#'
#' @examples
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_y_axis_line()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_y_axis_ticks()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_y_axis_labels()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_y_axis_title()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_y_axis()
#'
#' @export
remove_y_axis <- function(plot) {
check_tidyplot(plot)
Expand Down Expand Up @@ -97,6 +154,22 @@ remove_y_axis_title <- function(plot) {

#' Remove plot area padding
#' @inherit common_arguments
#'
#' @examples
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_mean_bar() %>%
#' remove_plot_area_padding()
#' # Before removing plot area padding
#' animals %>%
#' tidyplot(x = weight, y = speed, color = family) %>%
#' add_data_points()
#' # After removing plot area padding
#' animals %>%
#' tidyplot(x = weight, y = speed, color = family) %>%
#' add_data_points() %>%
#' remove_plot_area_padding()
#'
#' @export
remove_plot_area_padding <- function(plot, force_continuous = FALSE) {
check_tidyplot(plot)
Expand Down
49 changes: 41 additions & 8 deletions R/themes.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@

#' Themes
#' @inherit common_arguments
#'
#' @examples
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_data_points() %>%
#' add_sem_bar() %>%
#' add_mean_dash() %>%
#' theme_tidyplot()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_data_points() %>%
#' add_sem_bar() %>%
#' add_mean_dash() %>%
#' theme_ggplot2()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_data_points() %>%
#' add_sem_bar() %>%
#' add_mean_dash() %>%
#' theme_minimal_xy()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_data_points() %>%
#' add_sem_bar() %>%
#' add_mean_dash() %>%
#' theme_minimal_x()
#' study %>%
#' tidyplot(x = treatment, y = score, color = treatment) %>%
#' add_data_points() %>%
#' add_sem_bar() %>%
#' add_mean_dash() %>%
#' theme_minimal_y()
#'
#' @export
theme_tidyplot <- function(plot, fontsize = 7) {
check_tidyplot(plot)
Expand Down Expand Up @@ -35,33 +68,33 @@ theme_minimal_xy <- function(plot, fontsize = 7) {
}
#' @rdname theme_tidyplot
#' @export
theme_minimal_y <- function(plot, fontsize = 7) {
theme_minimal_x <- function(plot, fontsize = 7) {
check_tidyplot(plot)
plot <- plot + ggplot2::theme_minimal()
if (get_variable(plot, "colour") == ".single_color")
plot <- plot %>% remove_legend()
plot %>% adjust_font(fontsize) +
ggplot2::theme(
axis.line.x = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.major.x = ggplot2::element_blank(),
axis.line.y = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.major.x = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.minor.x = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.minor.y = ggplot2::element_blank()
)
}
#' @rdname theme_tidyplot
#' @export
theme_minimal_x <- function(plot, fontsize = 7) {
theme_minimal_y <- function(plot, fontsize = 7) {
check_tidyplot(plot)
plot <- plot + ggplot2::theme_minimal()
if (get_variable(plot, "colour") == ".single_color")
plot <- plot %>% remove_legend()
plot %>% adjust_font(fontsize) +
ggplot2::theme(
axis.line.y = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.major.x = ggplot2::element_line(colour = "grey", linewidth = 0.15),
axis.line.x = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.major.x = ggplot2::element_blank(),
panel.grid.minor.x = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_line(colour = "grey", linewidth = 0.15),
panel.grid.minor.y = ggplot2::element_blank()
)
}
Expand Down
6 changes: 6 additions & 0 deletions R/tidycolor.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ c.tidycolor <- function(...) new_color_scheme(NextMethod())
#' @param x Character vector of hex colors. For example `x = c("#FF00FF", "#00FFFF")`.
#' @param name Name of the custom color scheme.
#' @inherit common_arguments
#'
#' @examples
#' new_color_scheme(c("#ECA669","#E06681","#8087E2","#E2D269"))
#' new_color_scheme(c("#ECA669","#E06681","#8087E2","#E2D269"),
#' name = "my_custom_color_scheme")
#'
#' @export
new_color_scheme <- function(x, name = "Untitled color scheme", reverse = FALSE) {
if (!is_hex_vector(x)) cli::cli_abort("{.arg x} must be a vector of hex colors.")
Expand Down
6 changes: 6 additions & 0 deletions man/new_color_scheme.Rd

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

11 changes: 11 additions & 0 deletions man/remove_legend.Rd

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

16 changes: 16 additions & 0 deletions man/remove_plot_area_padding.Rd

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

23 changes: 23 additions & 0 deletions man/remove_x_axis.Rd

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

23 changes: 23 additions & 0 deletions man/remove_y_axis.Rd

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

Loading

0 comments on commit 095e292

Please sign in to comment.