Code
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(patchwork)
- 1
-
this package provides my favorite
ggplot2
theme:theme_ipsum()
- 2
- because I was too lazy to find something better in the R Color Finder!
This document provides the latest results of a few analysis run by Celesta lab. It also showcases what’s possible to do with lumo
, a minimalist quarto template.
Date: October 17, 2024
Quarto is probably my favorite tool in the R/Python universe. Transforming your code and thoughts into a stunning report in one click always blows my mind.
While the default Quarto appearance looks great, there are a few tweaks I always apply to elevate the report even further. Rather than manually adding them each time, I bundled everything into a custom format called lumo.
Btw, you can learn how to master Quarto thanks to my online course: Productive R Workflow
This document aims at showcasing how versatile the lumo format is. With a few tweaks, I made it fit the brand of a specific company.
Make sure to load all your packages at the start of your document for clarity and consistency.
Notice that the code chunks are folded by default. You can adjust this behavior with the code-fold
option in the document’s YAML header.
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(patchwork)
ggplot2
theme: theme_ipsum()
I strongly advise to create a dataviz theme that fits your company’s brand. Here is one:
<- function() {
scale_color_brand <- c( "#00496FFF", "#0F85A0FF", "#EDD746FF", "#ED8B00FF", "#DD4124FF")
custom_palette scale_color_manual(values = custom_palette)
}
<- function() {
theme_brand theme_ipsum() +
theme(
plot.title = element_text(color = "red", size = 18, face = "bold"),
plot.subtitle = element_text(margin = margin(b = 20)),
axis.text.x = element_text(size = 7),
axis.text.y = element_text(size = 7),
plot.background = element_rect(fill = "#faf5f5", color = NA),
plot.caption = element_text(hjust = 0)
) }
And now let’s use it on a graph:
# Create scatter plot
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_brand() +
# Add title, subtitle, and caption (footer)
labs(
title = "Sepal Length vs Sepal Width",
subtitle = "The relationship between sepal length and sepal width is very strong inside\neach species. It's worth noting that Setosa is very different, with a higher\nsepal width and lower sepal length on average",
caption = "Author: Yan Holtz\nData: coming from the batch #23 on the 3rd of October"
+
)
# Add annotation
annotate("text", x = 7.5, y = 4.2, label = "Highest Sepal Width", color = "red", size = 4, hjust = 0) +
# Customize the theme (optional)
theme_brand()
For a complete list of features, visit the documentation!
Oh and since we’re here, did you know you can easily include a full width interactive map with leaflet
in a quarto doc? 😍
# Load the library.
library(leaflet)
# Make a map
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R")
It is very easy to insert an interactive table in your document thanks to the DT
package. The output allows to filter rows, search for something and sort using a specific columns!
Install the library with install.packages("DT")
. Then, just pass a dataframe to the datatable()
function to get a stunning interactive output!
library(DT)
data(iris)
# Make a table
datatable(iris, filter = "top")
I’ve added a bit of CSS to make the font smaller on DT tables by the way 🙂
It’s always good to have a grey section. Makes the document breath a bit. I’ve added a little utility class in Lumo
to make sure you can make sections like this very easily. Check the documentation!
Let’s use this space to render a little equation:
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
Lumo
is a great format, but to get the most out of it, you’ll need to master a few Quarto tricks!
The good news? I’ve compiled my 30+ favorite tips in one place. Just spend 30 minutes, and you’ll be up to speed with all of them!
For example, if you’re creating multiple plots, each highlighting different aspects of your dataset, consider using pills to organize them!
This is a good old scatterplot of the iris dataset. If you want to learn more about how to make graphs in R, visit my R graph gallery!
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point() +
theme_brand() +
scale_color_brand() +
labs(
title = "Sepal Length vs Sepal Width",
subtitle = "Relationship between 2 numerical variables.",
caption = "Author: Yan Holtz\nData: coming from the batch #44 on the 3rd of October"
)
You can also make a boxplot, but please do not hide the underlying distribution! This is widely explain in my data-to-viz.com project in case you’re not convinced 😋. Check the next tab to get convinced!
# create a dataset
<- data.frame(
data name=c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100) ),
value=c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) )
)
# Plot
%>%
data ggplot( aes(x=name, y=value, fill=name)) +
geom_boxplot() +
theme(
legend.position="none",
plot.title = element_text(size=11)
+
) xlab("") +
theme_brand() +
scale_color_brand() +
labs(
title = "A boxplot that hides the underlying distribution",
subtitle = "Never hide the underlying samples on a boxplot. You never know what's behind!?\n(check the next tab: with jittering)",
caption = "Author: Yan Holtz\nData: coming from the batch #44 on the 3rd of October"
)
See what’s happening now that the individual data points are visible! Would be a shame to miss this.
%>%
data ggplot( aes(x=name, y=value, fill=name)) +
geom_boxplot() +
geom_jitter(color="black", size=0.4, alpha=0.9) +
theme(
legend.position="none",
plot.title = element_text(size=11)
+
) xlab("") +
theme_brand() +
scale_color_brand() +
labs(
title = "A boxplot with jitter",
subtitle = "This boxplot does NOT hide the underlying distribution.\nIt is much more powerful and reveals some massive differences between groups!",
caption = "Author: Yan Holtz\nData: coming from the batch #44 on the 3rd of October"
)
Learn how to use and how to customize Lumo
here.
When creating a Quarto document, it’s a good practice to include details about your working environment.
This is easy to do—just call sessionInfo()
, and it will print out all your R and package versions. This simple step greatly enhances reproducibility.
sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.4.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Paris
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DT_0.33 leaflet_2.2.2 patchwork_1.2.0 viridis_0.6.5
[5] viridisLite_0.4.2 hrbrthemes_0.8.7 lubridate_1.9.3 forcats_1.0.0
[9] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[13] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.5 bslib_0.7.0 xfun_0.43
[4] htmlwidgets_1.6.4 tzdb_0.4.0 crosstalk_1.2.1
[7] vctrs_0.6.5 tools_4.4.0 generics_0.1.3
[10] curl_5.2.1 fansi_1.0.6 pkgconfig_2.0.3
[13] lifecycle_1.0.4 farver_2.1.1 compiler_4.4.0
[16] munsell_0.5.1 httpuv_1.6.15 fontquiver_0.2.1
[19] fontLiberation_0.1.0 sass_0.4.9 htmltools_0.5.8.1
[22] yaml_2.3.8 Rttf2pt1_1.3.12 jquerylib_0.1.4
[25] pillar_1.9.0 later_1.3.2 crayon_1.5.2
[28] extrafontdb_1.0 gfonts_0.2.0 cachem_1.0.8
[31] mime_0.12 fontBitstreamVera_0.1.1 tidyselect_1.2.1
[34] digest_0.6.35 stringi_1.8.4 labeling_0.4.3
[37] extrafont_0.19 fastmap_1.1.1 grid_4.4.0
[40] colorspace_2.1-0 cli_3.6.2 magrittr_2.0.3
[43] crul_1.4.2 utf8_1.2.4 withr_3.0.0
[46] gdtools_0.3.7 scales_1.3.0 promises_1.3.0
[49] timechange_0.3.0 rmarkdown_2.26 gridExtra_2.3
[52] hms_1.1.3 shiny_1.8.1.1 evaluate_0.23
[55] knitr_1.46 rlang_1.1.3 Rcpp_1.0.12
[58] xtable_1.8-4 glue_1.7.0 httpcode_0.3.0
[61] rstudioapi_0.16.0 jsonlite_1.8.8 R6_2.5.1
[64] systemfonts_1.0.6