if (!require(pacman)) install.packages("pacman")
pacman::p_load(tidyplots, tidyverse, dplyr)Tidyplots R Package Examples
Setup
Basic Bar Plot with Error Bars
Using the built-in study dataset.
study# A tibble: 20 × 7
treatment group dose participant age sex score
<chr> <chr> <chr> <chr> <dbl> <chr> <dbl>
1 A placebo high p01 23 female 2
2 A placebo high p02 45 male 4
3 A placebo high p03 32 female 5
4 A placebo high p04 37 male 4
5 A placebo high p05 24 female 6
6 B placebo low p06 23 female 9
7 B placebo low p07 45 male 8
8 B placebo low p08 32 female 12
9 B placebo low p09 37 male 15
10 B placebo low p10 24 female 16
11 C treatment high p01 23 female 32
12 C treatment high p02 45 male 35
13 C treatment high p03 32 female 24
14 C treatment high p04 37 male 45
15 C treatment high p05 24 female 56
16 D treatment low p06 23 female 23
17 D treatment low p07 45 male 25
18 D treatment low p08 32 female 21
19 D treatment low p09 37 male 22
20 D treatment low p10 24 female 23
study |>
tidyplot(x = group, y = score, color = dose) |>
add_mean_bar(alpha = 0.3) |>
add_sem_errorbar() |>
add_data_points() |>
add_test_asterisks(hide_info = TRUE)Horizontal Bar Plot
study |>
tidyplot(x = score, y = treatment, color = treatment) |>
add_mean_bar(alpha = 0.3) |>
add_sem_errorbar() |>
add_data_points()Spendings Bar Plot with Custom Colors
spendings |>
tidyplot(x = category, y = amount, color = category) |>
add_sum_bar() |>
adjust_x_axis(rotate_labels = TRUE) |>
sort_x_axis_levels() |>
remove_legend() |>
adjust_colors(new_colors = c("Health" = "#ff3b30"))Warning: No shared levels found between `names(values)` of the manual scale and the
data's colour values.
Multiple Custom Colors
all_colors <- c(
"Health" = "#ff3b30",
"Food" = "blue",
"Housing" = "green"
)
spendings |>
tidyplot(x = category, y = amount, color = category) |>
add_sum_bar() |>
adjust_x_axis(rotate_labels = TRUE) |>
sort_x_axis_levels() |>
remove_legend() |>
adjust_colors(new_colors = all_colors)Warning: No shared levels found between `names(values)` of the manual scale and the
data's colour values.
Violin Plot with Beeswarm
study |>
tidyplot(x = treatment, y = score, color = treatment) |>
add_violin() |>
add_data_points_beeswarm()Reorder and Rename Axis Levels
study |>
tidyplot(x = treatment, y = score, color = treatment) |>
add_violin() |>
add_data_points_beeswarm() |>
reorder_x_axis_levels("C", "D", "A", "B") |>
rename_x_axis_levels(new_names = c(
"A" = "Dog",
"B" = "Cat",
"C" = "Hamster",
"D" = "Dinosaur"
))Scientific Notation in Labels
study |>
tidyplot(x = treatment, y = score, color = treatment) |>
add_violin() |>
add_data_points_beeswarm() |>
reorder_x_axis_levels("C", "D", "A", "B") |>
rename_x_axis_levels(new_names = c(
"A" = "$IFN*gamma$",
"B" = "$TNF*alpha$",
"C" = "$H[2]*O$",
"D" = "$C[8]*H[10]*N[4]*O[2]$"
)) |>
adjust_size(width = 100, height = 100)Statistical Tests with P-values
study |>
tidyplot(x = treatment, y = score, color = treatment) |>
add_mean_dash() |>
add_sem_errorbar() |>
add_data_points() |>
add_test_pvalue(
hide_info = TRUE,
comparisons = list(c(1, 3), c(2, 4), c(2, 3), c(1, 4))
)Reference Lines
animals |>
tidyplot(x = weight, y = speed) |>
add_reference_lines(
x = 4000,
y = c(100, 200),
linetype = "dotdash"
) |>
add_data_points() |>
adjust_size(width = 100, height = 100)Gene Expression Analysis
gene_expression |>
filter(external_gene_name %in% c("Apol6", "Col5a3", "Bsn", "Fam96b", "Mrps14", "Tma7")) |>
tidyplot(x = sample_type, y = expression, color = condition) |>
add_violin() |>
add_data_points_beeswarm(white_border = TRUE) |>
adjust_x_axis_title("") |>
remove_legend() |>
add_test_asterisks(hide_info = TRUE, bracket.nudge.y = 0.3) |>
adjust_colors(colors_discrete_ibm) |>
adjust_y_axis_title("Gene expression") |>
adjust_size(width = 35, height = 25) |>
split_plot(by = external_gene_name, ncol = 2)✔ split_plot: split into 6 plots across 1 page
PCA Plot with Ellipses
df <- read_csv("https://tidyplots.org/data/pca-plot.csv")
df |>
tidyplot(x = pc1, y = pc2, color = group) |>
add_data_points(size = 1.3, white_border = TRUE) |>
add_ellipse() |>
adjust_x_axis_title(paste0("Component 1 (", round(df$pc1_var * 100, digits = 1), "%)")) |>
adjust_y_axis_title(paste0("Component 2 (", round(df$pc2_var * 100, digits = 1), "%)")) |>
adjust_colors(colors_discrete_apple) |>
adjust_legend_title("Group")Custom Style Function
Define a reusable style and set it as a global option.
# Define Style
my_style <- function(x) {
x |>
adjust_colors(colors_discrete_candy) |>
adjust_font(family = "mono") |>
adjust_size(height = 30) |>
adjust_size(width = 100, height = 10)
}
# Set global options
tidyplots_options(my_style = my_style)
study |>
tidyplot(x = group, y = score, color = dose) |>
add_mean_bar()Saving Plots
Example of saving plots at different stages of the pipeline.
study |>
tidyplot(x = treatment, y = score, color = treatment) |>
save_plot("st1.png") |>
add_mean_dash() |>
save_plot("st2.png") |>
add_sem_errorbar() |>
save_plot("st3.png") |>
add_data_points() |>
save_plot("st4.png") |>
add_test_asterisks(hide_info = FALSE) |>
save_plot("st5.png")