Jan Broder Engler
@jbengler.de
1.3K followers 640 following 440 posts
Scientist and software developer. Creator of tidyplots.org. Website jbengler.de.
Posts Media Videos Starter Packs
Pinned
jbengler.de
Hey educators, tidyplots.org is great for teaching students data visualization because it is

🕊️ Free and open-source
🚀 Easy, intuitive and fast
💾 Loaded with demo datasets
🏆 Embracing best practices
🌈 Beautiful

#rstats #phd #dataviz #education
jbengler.de
This is how you can add normal data ellipses in #tidyplots 🐣

#rstats #dataviz #phd
library(tidyplots)

pca |>
  tidyplot(x = pc1, y = pc2, color = group) |>
  add_data_points() |>
  add_ellipse()
jbengler.de
Being free and open-source, this is how you can support #tidyplots ❤️

tidyplots.org
github.com/jbengler/tid...

#rstats #dataviz #phd
jbengler.de
This is how you can preview a subset of the dataset in #tidyplots 🔎

#rstats #dataviz #phd
library(tidyplots)

gene_expression |> 
  tidyplot(x = group, y = expression, color = group) |> 
  add_violin() |> 
  add_data_points_beeswarm(white_border = TRUE) |> 
  view_plot(data = filter_rows(external_gene_name == "Apol6"),
            title = "Apol6")
jbengler.de
This is how you can test selected comparisons in #tidyplots

#rstats #dataviz #phd
library(tidyplots)

study |>
  tidyplot(x = treatment, y = score, color = treatment) |>
  add_mean_dash() |>
  add_sem_errorbar() |>
  add_data_points() |>
  add_test_pvalue(comparisons = list(c(1,3),c(2,4)))
jbengler.de
This is how you can do paired testing in #tidyplots 🍒

#rstats #dataviz #phd
library(tidyplots)

x <- c(2.3, 4.5, 6.3, 3.4, 7.8, 6.7)
df <- data.frame(
  x = c(x, x + c(0.8, 0.75)),
  group = paste0("g", rep(c(1, 2), each = 6)),
  batch = paste0("b", c(1:6, 1:6)),
  shuffle = paste0("c", c(1:6, 6:1))
)

df |>
  tidyplot(group, x, color = group) |>
  add_boxplot() |>
  add_data_points() |>
  add_test_pvalue(paired_by = batch) |>
  add_line(group = batch, color = "black")
jbengler.de
This is how you can adjust the plot size in #tidyplots 🔥

All measures are in mm.

#rstats #dataviz #phd
library(tidyplots)

study |> 
  tidyplot(x = group, y = score, color = group) |> 
  add_median_bar(alpha = 0.3) |> 
  add_median_dash() |> 
  add_data_points_beeswarm(white_border = TRUE) |> 
  remove_x_axis_labels() |> 
  adjust_size(width = 20, height = 50)
jbengler.de
This is how you can adjust colors in #tidyplots 🌈

#rstats #dataviz #phd
library(tidyplots)
library(patchwork)

p1 <- 
  energy |> 
  tidyplot(x = year, y = energy, color = energy_type) |> 
  add_barstack_absolute() |> 
  adjust_colors(colors_discrete_apple)

p2 <- p1 |> adjust_colors(colors_discrete_metro)

wrap_plots(p1, p2, ncol = 1)
jbengler.de
This is how you can do stacked bar charts in #tidyplots 🐝

#rstats #dataviz #phd
library(tidyplots)

energy |> 
  tidyplot(x = year, y = energy, color = energy_type) |> 
  add_barstack_absolute()
jbengler.de
Here are some #tidyplots use cases for data journalism 🔥

Complete with data and code 🚀

tidyplots.org/use-cases/#d...

#rstats #dataviz #phd
jbengler.de
Thanks for leading me to the right path, man!
jbengler.de
This is how you can do horizontal bars in #tidyplots 😉

#rstats #dataviz #phd
library(tidyplots)

spendings |> 
  tidyplot(x = amount, y = category, color = category) |> 
  add_sum_bar(alpha = 0.2) |> 
  add_sum_dash() |> 
  add_sum_value(accuracy = 1, color = "black") |> 
  sort_y_axis_levels() |> 
  remove_legend()
jbengler.de
You are welcome 😂
jbengler.de
This is how you can sort the x-axis in #tidyplots 🤩

#rstats #dataviz #phd
library(tidyplots)

spendings |> 
  tidyplot(x = category, y = amount, color = category) |> 
  add_sum_bar(alpha = 0.2) |> 
  add_sum_dash() |> 
  add_sum_value(accuracy = 1, color = "black") |> 
  adjust_x_axis(rotate_labels = TRUE) |> 
  sort_x_axis_levels()
jbengler.de
This is how you can add sum values in #tidyplots 🤩

#rstats #dataviz #phd
library(tidyplots)

spendings |> 
  tidyplot(x = category, y = amount, color = category) |> 
  add_sum_bar(alpha = 0.2) |> 
  add_sum_dash() |> 
  add_sum_value(accuracy = 1, color = "black") |> 
  adjust_x_axis(rotate_labels = TRUE)
jbengler.de
This is how you can generate donut charts in #tidyplots 🍩

#rstats #dataviz #phd
library(tidyplots)

energy |>
  tidyplot(y = energy, color = energy_source) |>
  add_donut()
jbengler.de
Right. I am looking for a ggsave() that does not need to be provided with the dimensions but rather extracts them from the absolutely sized plot itself.

Here are two implementations of this
- teunbrand.github.io/ggh4x/refere...
- jbengler.github.io/tidyplots/re...
jbengler.de
I see. Thank you for the link!
jbengler.de
For me it works with ggplots and tidyplots.

However, the display of plots with absolute dimensions could be improved.
jbengler.de
Patchwork was not initially designed to handle plots with absolute dimensions. But it would be really great to be able to use it for arranging plots of different predefined sizes.
jbengler.de
The ggsave() function could check whether the plot has absolute dimensions. If so, the device size should be automatically adapted to fit the plot.
jbengler.de
Currently, the plot gets either cut off or has a lot of white space around.

Great would be a "scale to fit" option.

Similar to a responsive web image.

www.w3schools.com/css/css_rwd_...
jbengler.de
Now that ggplot2 4.0.0 supports absolute plot dimensions, we need better support in

1. Positron & RStudio viewer pane
2. ggsave()
3. quarto
4. shiny
5. patchwork

😜

#rstats #dataviz #phd