diff --git a/_freeze/posts/2000-01-01-quarto-post-example/index/execute-results/html.json b/_freeze/posts/2000-01-01-quarto-post-example/index/execute-results/html.json index 40d531c..b69c25f 100644 --- a/_freeze/posts/2000-01-01-quarto-post-example/index/execute-results/html.json +++ b/_freeze/posts/2000-01-01-quarto-post-example/index/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "187a40270ae9ea177cb0b2cdf34fec9d", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Quarto post example\"\nsubtitle: \"Yep, this is a quarto post example\"\ndescription: Description is a longer text of what your post is about, or anything else. This particular\n post is about make an example of a quarto post showing what I like about quarto.\ndate: 2000-01-01\ndate-modified: \"2024-03-28\"\ncategories: [testing, dont-forget]\nimage: images/preview.jpg\nfig-format: svg\necho: fenced \n---\n\n\n\n\n## Some quicks points\n\n1. Titles in the main are with `##`: Yes, the `

` title is of the first/main title.\n1. This is a [link](https://quarto.org/docs/reference/formats/html.html#links). Another link is \nhttps://github.com/quarto-dev/quarto-cli\n\n## Testing htmlwidgets\n\n\n::: {.cell}\n\n````{.cell-code}\n```{{r}}\nlibrary(highcharter)\nlibrary(palmerpenguins)\n\nhc <- hchart(\n penguins, \n \"point\", \n hcaes(bill_length_mm, bill_depth_mm, group = species)\n )\n\nhc\n```\n````\n\n::: {.cell-output-display}\n\n```{=html}\n
\n\n```\n\n:::\n:::\n\n\n## Types of column\n\nMore info in https://quarto.org/docs/authoring/article-layout.html\n\nHow to use: In the chunk specify with `column` argument.\n\n```r\n#| column: page\n```\n\nNow, list of main column types.\n\n::: {.layout-example .column-body}\n.column-body\n:::\n\n::: {.layout-example .column-body-outset}\n.column-body-outset\n:::\n\n::: {.layout-example .column-page}\n.column-page\n:::\n\n::: {.layout-example .column-page-inset}\n.column-page-inset\n:::\n\n::: {.layout-example .column-screen}\n.column-screen\n:::\n\n::: {.layout-example .column-screen-inset}\n.column-screen-inset\n:::\n\n::: {.layout-example .column-screen-inset-shaded}\n.column-screen-inset-shaded\n:::\n\n## Figures\n\nAccording to https://r4ds.hadley.nz/quarto.html#sec-figures:\n\n\n::: {.cell}\n\n````{.cell-code}\n```{{r}}\nlibrary(ggplot2)\nlibrary(ggforce)\n\np <- ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm)) +\n # geom_smooth(aes(color = species), method = \"lm\") +\n geom_mark_hull(\n aes(filter = species == \"Gentoo\", label = species),\n description = \"A species with long flippers\",\n color = \"gray95\",\n fill = \"gray80\",\n concavity = 4,\n size = 20\n ) +\n geom_point(aes(fill = species), color = \"gray80\", size = 3, shape = 21) +\n scale_fill_viridis_d(option = \"B\", begin = 0.1, end = 0.9) +\n # scale_color_viridis_d(option = \"B\", begin = 0.1, end = 0.9) +\n labs(\n title = \"Flipper and bill length\",\n subtitle = \"Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER\",\n x = \"Flipper length (mm)\",\n y = \"Bill length (mm)\",\n fill = \"Penguin species\",\n captions = \"Source: https://allisonhorst.github.io/palmerpenguins/\" \n )\n\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.svg)\n:::\n:::\n\n::: {.cell .column-body-outset}\n\n````{.cell-code}\n```{{r}}\n#| fig-width: 6\n#| out-width: \"100%\"\n#| fig-asp: 0.618\n#| column: body-outset\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.svg){width=100%}\n:::\n:::\n\n::: {.cell .column-body-outset layout-align=\"center\"}\n\n````{.cell-code}\n```{{r}}\n#| out-width: \"100%\"\n#| fig-align: \"center\"\n#| column: body-outset\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-1.svg){fig-align='center' width=100%}\n:::\n:::\n\n::: {.cell .column-page layout-align=\"center\"}\n\n````{.cell-code}\n```{{r}}\n#| out-width: \"100%\"\n#| fig-align: \"center\"\n#| column: page\np + theme_minimal(base_size = 7)\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-5-1.svg){fig-align='center' width=100%}\n:::\n:::\n\n\n## Marging content\n\nContent in the right:\n\n\n::: {.cell .column-margin}\n\n````{.cell-code}\n```{{r, out.height=300}}\n#| column: margin\n# This will be in margin so make points smaller\n# #| column: margin\n# out.height=300\nhc |> \n hc_plotOptions(\n scatter = list(showInLegend = FALSE, marker = list(radius = 2)) \n )\n```\n````\n\n::: {.cell-output-display}\n\n```{=html}\n
\n\n```\n\n:::\n:::\n\n\n::: {.layout-example .column-margin}\n.column-margin\n:::\n\n\n\n\n\n", + "markdown": "---\ntitle: \"Quarto post example\"\nsubtitle: \"Yep, this is a quarto post example\"\ndescription: Description is a longer text of what your post is about, or anything else. This particular\n post is about make an example of a quarto post showing what I like about quarto.\ndate: 2000-01-01\ndate-modified: \"2024-03-28\"\ncategories: [testing, dont-forget]\nimage: images/preview.jpg\nfig-format: svg\necho: fenced \n---\n\n\n\n\n## Some quicks points\n\n1. Titles in the main are with `##`: Yes, the `

` title is of the first/main title.\n1. This is a [link](https://quarto.org/docs/reference/formats/html.html#links). Another link is \nhttps://github.com/quarto-dev/quarto-cli\n\n## Testing htmlwidgets\n\n\n::: {.cell}\n\n````{.cell-code}\n```{{r}}\nlibrary(highcharter)\nlibrary(palmerpenguins)\n\nhc <- hchart(\n penguins, \n \"point\", \n hcaes(bill_length_mm, bill_depth_mm, group = species)\n )\n\nhc\n```\n````\n\n::: {.cell-output-display}\n\n```{=html}\n
\n\n```\n\n:::\n:::\n\n\n## Types of column\n\nMore info in https://quarto.org/docs/authoring/article-layout.html\n\nHow to use: In the chunk specify with `column` argument.\n\n```r\n#| column: page\n```\n\nNow, list of main column types.\n\n::: {.layout-example .column-body}\n.column-body\n:::\n\n::: {.layout-example .column-body-outset}\n.column-body-outset\n:::\n\n::: {.layout-example .column-page}\n.column-page\n:::\n\n::: {.layout-example .column-page-inset}\n.column-page-inset\n:::\n\n::: {.layout-example .column-screen}\n.column-screen\n:::\n\n::: {.layout-example .column-screen-inset}\n.column-screen-inset\n:::\n\n::: {.layout-example .column-screen-inset-shaded}\n.column-screen-inset-shaded\n:::\n\n## Figures\n\nAccording to https://r4ds.hadley.nz/quarto.html#sec-figures:\n\n\n::: {.cell}\n\n````{.cell-code}\n```{{r}}\nlibrary(ggplot2)\nlibrary(ggforce)\n\np <- ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm)) +\n # geom_smooth(aes(color = species), method = \"lm\") +\n geom_mark_hull(\n aes(filter = species == \"Gentoo\", label = species),\n description = \"A species with long flippers\",\n color = \"gray95\",\n fill = \"gray80\",\n concavity = 4,\n size = 20\n ) +\n geom_point(aes(fill = species), color = \"gray80\", size = 3, shape = 21) +\n scale_fill_viridis_d(option = \"B\", begin = 0.1, end = 0.9) +\n # scale_color_viridis_d(option = \"B\", begin = 0.1, end = 0.9) +\n labs(\n title = \"Flipper and bill length\",\n subtitle = \"Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER\",\n x = \"Flipper length (mm)\",\n y = \"Bill length (mm)\",\n fill = \"Penguin species\",\n captions = \"Source: https://allisonhorst.github.io/palmerpenguins/\" \n )\n\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.svg)\n:::\n:::\n\n::: {.cell .column-body-outset}\n\n````{.cell-code}\n```{{r}}\n#| fig-width: 6\n#| out-width: \"100%\"\n#| fig-asp: 0.618\n#| column: body-outset\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.svg){width=100%}\n:::\n:::\n\n::: {.cell .column-body-outset layout-align=\"center\"}\n\n````{.cell-code}\n```{{r}}\n#| out-width: \"100%\"\n#| fig-align: \"center\"\n#| column: body-outset\np\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-1.svg){fig-align='center' width=100%}\n:::\n:::\n\n::: {.cell .column-page layout-align=\"center\"}\n\n````{.cell-code}\n```{{r}}\n#| out-width: \"100%\"\n#| fig-align: \"center\"\n#| column: page\np + theme_minimal(base_size = 7)\n```\n````\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-5-1.svg){fig-align='center' width=100%}\n:::\n:::\n\n\n## Marging content\n\nContent in the right:\n\n\n::: {.cell .column-margin}\n\n````{.cell-code}\n```{{r, out.height=300}}\n#| column: margin\n# This will be in margin so make points smaller\n# #| column: margin\n# out.height=300\nhc |> \n hc_plotOptions(\n scatter = list(showInLegend = FALSE, marker = list(radius = 2)) \n )\n```\n````\n\n::: {.cell-output-display}\n\n```{=html}\n
\n\n```\n\n:::\n:::\n\n\n::: {.layout-example .column-margin}\n.column-margin\n:::\n\n\n\n\n\n", "supporting": [ "index_files" ], diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/execute-results/html.json b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/execute-results/html.json index 85dada2..1cab0ae 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/execute-results/html.json +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/execute-results/html.json @@ -1,7 +1,8 @@ { - "hash": "608ebf0cb69f0dfa3c54d36f171639b0", + "hash": "00639c1ab0343f417cc61ade49526b1e", "result": { - "markdown": "---\ntitle: \"My favorite `ggplot2` packages with examples\"\nsubtitle: I have to put things somewhere so I don't forget them\ndate: 2023-07-23\ndate-modified: \"2024-03-26\"\ncategories: [minipost, ggplot2, dont-forget]\nimage: images/preview.png \neditor_options: \n chunk_output_type: console\n---\n\n\n\n\nThe idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.\n\n* Typography: `{showtext}`.\n* Annotations: `{ggrepel}`, `{gghighlight}`, `{ggforce}`, `{ggfittext}`.\n* Extra features: `{scales}` and -again- `{ggforce}`.\n* Tree-objects: `{ggparty}` and `{parttree}`.\n\nWhere do we start from? Define some data and some defaults plots that we would have and would like to modify. \n\n\n::: {.cell .column-page layout-ncol=\"3\"}\n\n```{.r .cell-code}\nlibrary(tidyverse)\nlibrary(palmerpenguins)\n\nset.seed(123)\n\nmtcars <- mtcars |>\n rownames_to_column(var = \"name\") |> \n as_tibble() |> \n sample_n(20)\n\npenguins <- penguins |> \n filter(complete.cases(penguins)) \n\np1 <- ggplot(mtcars, aes(wt, mpg, label = name)) +\n geom_point(color = \"cyan4\", size = 2)\np1\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-1.svg)\n:::\n\n```{.r .cell-code}\np2 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +\n geom_point(aes(color = species), size = 2)\np2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-2.svg)\n:::\n\n```{.r .cell-code}\np3 <- penguins |> \n count(species) |> \n ggplot(aes(species, n)) +\n geom_col()\np3\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-3.svg)\n:::\n:::\n\n\nIn my particuar case I like `theme_minimal` theme and legend at the bottom.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntheme_set(\n theme_minimal() +\n theme(\n plot.title = element_text(face = \"plain\"),\n plot.background = element_rect(fill = \"#fcfcfc\", colour = NA),\n panel.background = element_rect(fill = \"#fcfcfc\", colour = NA),\n legend.position = \"bottom\",\n legend.key.width = unit(1.5, \"cm\")\n )\n)\n\ncolors <- c(\"darkorange\",\"darkorchid\",\"cyan4\")\n```\n:::\n\n\nWe're ready to go!\n\n\n## `{showtext}`\n\nSource:[ https://github.com/yixuan/showtext](https://github.com/yixuan/showtext).\n\nThis package makes easy to use typography! Do you want some typography from https://fonts.google.com/? You want it? You got it!\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(showtext)\n\nfont_add_google(\"IBM Plex Sans\", family = \"ibm\")\n\nshowtext_auto()\n\np2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.svg)\n:::\n:::\n\n\n\n## `{ggrepel}`\n\nSource: [https://ggrepel.slowkow.com/articles/examples.html](https://ggrepel.slowkow.com/articles/examples.html).\n\nThis package has been on CRAN/github for a while now. Package that in combination with the `data` argument can make simple and effective annotations.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggrepel)\n\np_repel1 <- p1 +\n geom_text_repel(color = \"gray40\", family = \"ibm\")\n\np_repel1\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-1.svg)\n:::\n\n```{.r .cell-code}\np_repel2 <- p1 +\n geom_text_repel(\n data = ~ filter(.x, mpg > 30), \n color = \"gray40\", \n family = \"ibm\",\n force = 20\n )\n\np_repel2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-2.svg)\n:::\n:::\n\n\n\n## `{gghighlight}`\n\nSource: [https://yutannihilation.github.io/gghighlight/articles/gghighlight.html](https://yutannihilation.github.io/gghighlight/articles/gghighlight.html).\n\n- This package is plug and play if you have to highlight data with a specific filter. \n- Is important to know this package don't work like the usual `geom_*` + `aes()`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(gghighlight) \n\np1 +\n gghighlight(\n # interest subset\n mpg > 30,\n # additional parameters\n label_key = name,\n keep_scales = TRUE,\n label_params = list(color = \"gray40\", label.r = 0, fill = \"gray95\"),\n # can change previous parameters\n unhighlighted_params = list(size = 1.5, color = \"darkorchid\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-5-1.svg)\n:::\n:::\n\n\n\n## `{ggforce}`\n\nSource: [https://ggforce.data-imaginist.com/](https://ggforce.data-imaginist.com/).\n\n- `{ggforce}` takes annotations to the next level.\n- A lot of other functionalities like new geoms, new facets!\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggforce)\n\np1 +\n geom_mark_hull(\n aes(filter = mpg > 30, label = \"Interesting\"),\n description = \"Lightweight vehicles have high performance.\",\n color = \"gray70\",\n fill = \"gray90\",\n concavity = 5,\n # control width text\n label.minwidth = unit(100, \"mm\"),\n # how much distance before show legend\n label.buffer = unit(2.5, \"mm\"),\n label.colour = \"gray30\",\n label.family = \"ibm\"\n ) +\n # use circle for points\n geom_mark_circle(\n aes(filter = wt > 3.75, label = NULL, description = name),\n color = \"gray70\",\n fill = \"transparent\",\n label.fontsize = 10,\n label.family = \"ibm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-6-1.svg)\n:::\n:::\n\n\nAnd other `geom_mark_*` like hull, circle, ellpse, rect.\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\np2 + \n geom_mark_ellipse(\n aes(fill = species, label = species),\n alpha = 0.1,\n color = \"transparent\", # a nice touch sometimes! (imho)\n label.colour = \"gray30\",\n label.family = \"ibm\",\n label.fontsize = 8,\n # label.fontface = \"plain\",\n # this is just for blogpost\n expand = unit(-5, \"mm\"),\n radius = unit(5, \"mm\")\n ) +\n geom_mark_circle(\n aes(\n filter = coalesce(bill_length_mm, 0) == max(bill_length_mm, na.rm = TRUE),\n label = NULL, \n description = \"A rare penguin!\"\n ),\n color = \"gray70\",\n fill = \"transparent\",\n label.fontsize = 8,\n label.family = \"ibm\"\n ) +\n theme(legend.position = \"none\") +\n labs(x = NULL, y = NULL)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-7-1.svg){fig-align='center'}\n:::\n:::\n\n\nNow imagine we used a $K$-means algorithm:\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\ndcenters <- penguins |>\n select(species, bill_length_mm, bill_depth_mm) |> \n filter(!is.na(bill_length_mm)) |> \n filter(!is.na(bill_depth_mm)) |> \n group_by(species) |> \n summarise(across(everything(), median)) |> \n select(-species) |> \n mutate(cluster = as.character(row_number()))\n\ndcenters\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 3 × 3\n bill_length_mm bill_depth_mm cluster\n \n1 38.8 18.4 1 \n2 49.6 18.4 2 \n3 47.4 15 3 \n```\n:::\n\n```{.r .cell-code}\nbnd <- penguins |>\n summarise(\n min(bill_length_mm, na.rm = TRUE) - 1,\n max(bill_length_mm, na.rm = TRUE) + 1,\n min(bill_depth_mm, na.rm = TRUE) - 1,\n max(bill_depth_mm, na.rm = TRUE) + 1\n ) |> \n as.list() |> \n unlist() |> \n as.vector()\n\np2 +\n geom_voronoi_tile(\n aes(fill = cluster, group = -1),\n data = dcenters, alpha = 0.2, bound = bnd\n ) +\n geom_voronoi_segment(\n aes(group = -1),\n data = dcenters, color = \"gray90\", bound = bnd\n ) +\n xlim(bnd[1], bnd[2]) + \n ylim(bnd[3], bnd[4]) +\n scale_fill_viridis_d(direction = -1, option = \"C\") +\n # its better put point over all layers\n geom_point(\n aes(color = species), \n size = 2,\n shape = 21, \n color = \"gray90\"\n ) +\n theme(legend.position = \"right\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-8-1.svg){fig-align='center'}\n:::\n:::\n\n\n## `{ggfittext}`\n\nSource: [https://github.com/wilkox/ggfittext](https://github.com/wilkox/ggfittext#bar-plots).\n\nThis package have a lot of features to work with strings in ggplot objects, particulary\nwhen you use treemaps.\n\nA love the simple and useful function `geom_bar_text()` + `geom_col()` combo.\n\n\n\n::: {.cell .column-page layout-ncol=\"3\"}\n\n```{.r .cell-code}\nlibrary(ggfittext)\n\np3 <- penguins |> \n count(species) |> \n mutate(n = 100 * n) |> \n ggplot(aes(species, n)) +\n geom_col(width = 0.35, fill = \"cyan4\") \n\np3 +\n geom_bar_text()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-1.svg)\n:::\n\n```{.r .cell-code}\np3 +\n geom_bar_text(\n formatter = scales::comma_format(), \n padding.y = grid::unit(2.5, \"mm\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-2.svg)\n:::\n\n```{.r .cell-code}\np3 + \n geom_bar_text(\n formatter = scales::comma_format(), \n place = \"bottom\",\n padding.y = grid::unit(2.5, \"mm\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-3.svg)\n:::\n:::\n\n\n\n## `{scales}`\n\nSource: [https://scales.r-lib.org/](https://scales.r-lib.org/).\n\n\nYou use this package using a `label_*` function in the `labels` argument of\n`scale_*` function.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(scales) # comma dollar label_comma\n\nx <- c(123, 456678, 100000)\n\nc(comma(x), dollar(x))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] \"123\" \"456,678\" \"100,000\" \"$123\" \"$456,678\" \"$100,000\"\n```\n:::\n\n```{.r .cell-code}\n# I know the correct alternative is Mpg\nmiles_per_gallon <- label_comma(suffix = \" mi/gal\")\n\nwt_lbl <- label_comma(scale = 1000, suffix = \" lbs\")\n\np1 +\n scale_y_continuous(\n labels = miles_per_gallon, \n name = \"fuel consumption\"\n ) +\n scale_x_continuous(\n labels = wt_lbl, \n name = \"weigth\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-10-1.svg)\n:::\n:::\n\n\n## `{ggparty}`\n\nSource: [https://github.com/martin-borkovec/ggparty](https://github.com/martin-borkovec/ggparty).\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(partykit) # ctree\nlibrary(ggparty)\n\npenguinct <- ctree(\n species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g + sex,\n data = penguins\n )\n\nautoplot(penguinct)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-11-1.svg)\n:::\n:::\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\nggparty(penguinct) +\n geom_edge(color = \"gray80\") +\n geom_edge_label(color = \"gray50\", size = 4) +\n geom_node_label(\n aes(label = splitvar),\n color = \"gray30\",\n label.col = NA, # no box\n size = 4,\n label.padding = unit(0.5, \"lines\"),\n ids = \"inner\"\n ) +\n geom_node_plot(\n gglist = list(\n geom_point(\n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 1, alpha = 0.5\n ),\n scale_color_viridis_d(end = 0.9),\n guides(color = guide_legend(override.aes = list(size = 5))), \n theme_minimal(),\n theme(axis.text = element_text(size = 7)),\n labs(x = NULL, y = NULL)\n ),\n scales = \"fixed\",\n id = \"terminal\"\n ) +\n geom_node_label(\n aes(label = sprintf(\"Node %s (n = %s)\", id, nodesize)),\n ids = \"terminal\",\n size = 3,\n label.col = NA, # no box\n nudge_y = 0.01\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-12-1.svg){fig-align='center'}\n:::\n:::\n\n\n\n## `{parttree}`\n\nSource: [https://github.com/grantmcdermott/parttree](https://github.com/grantmcdermott/parttree).\n\nI use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# remotes::install_github(\"grantmcdermott/parttree\")\nlibrary(parttree)\n\n# 2 independent variables\npenguinct2 <- ctree(\n species ~ bill_length_mm + bill_depth_mm,\n data = penguins,\n control = ctree_control(maxdepth = 3)\n )\n\nggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +\n geom_parttree(\n data = penguinct2, \n aes(fill = species),\n alpha = 0.2, \n color = \"gray60\",\n ) + \n geom_point(aes(col = species)) +\n theme_minimal()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-13-1.svg)\n:::\n:::\n\n\n## `{ggparty}` + `{parttree}` \n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\ndpred_node <- penguins |>\n select(species, bill_length_mm, bill_depth_mm) |>\n mutate(\n id = predict(penguinct2, type = \"node\", newdata = penguins),\n species_pred = predict(penguinct2, newdata = penguins)\n ) |>\n group_by(id) |>\n summarise(\n species = unique(species_pred),\n bill_length_mm = mean(bill_length_mm),\n bill_depth_mm = mean(bill_depth_mm)\n )\n\ndpred_node\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 5 × 4\n id species bill_length_mm bill_depth_mm\n \n1 3 Adelie 37.4 15.2\n2 4 Adelie 38.5 18.4\n3 6 Gentoo 47.4 14.9\n4 8 Adelie 43.0 18.2\n5 9 Chinstrap 49.5 18.4\n```\n:::\n\n```{.r .cell-code}\ndparttree <- parttree(penguinct2)\ndparttree <- dparttree |> \n as_tibble() |> \n rename(id = node) |> \n select(-path)\n\nggp <- ggparty(penguinct2) \n\nggp$data <- ggp$data |> \n as_tibble() |> \n left_join(\n dpred_node |> select(id, species),\n by = join_by(id)\n )\n\nggp +\n geom_edge(color = \"gray80\") +\n geom_edge_label(color = \"gray50\", size = 4) +\n geom_node_label(\n aes(label = str_replace_all(splitvar, \"_\", \" \")),\n color = \"gray30\",\n label.col = NA, # no box\n size = 4,\n label.padding = unit(0.5, \"lines\"),\n ids = \"inner\"\n ) +\n geom_node_plot(\n gglist = list(\n geom_point(\n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 1, alpha = 0.5\n ),\n geom_parttree(\n data = penguinct2, \n aes(fill = species),\n alpha = 0.1, \n color = \"gray60\",\n ),\n \n geom_point(\n data = dpred_node, \n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 3\n ),\n \n geom_rect(\n data = dparttree, \n aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = species),\n alpha = 0.5, \n color = \"gray40\",\n ),\n \n scale_fill_manual(values = colors),\n scale_color_manual(values = colors),\n # scale_color_viridis_d(end = 0.9),\n # scale_fill_viridis_d(end = 0.9),\n guides(color = guide_legend(override.aes = list(size=5))),\n theme_minimal(),\n theme(axis.text = element_text(size = 7)),\n labs(x = NULL, y = NULL)\n ),\n scales = \"fixed\",\n # id = \"all\"\n id = \"terminal\"\n ) +\n geom_node_label(\n aes(label = str_glue(\"{species} (#{id}, n = {nodesize})\")),\n ids = \"terminal\",\n size = 3,\n family = \"ibm\",\n label.col = NA, # no box\n nudge_y = 0.01\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-14-1.svg){fig-align='center'}\n:::\n:::\n\n\n## `{patchwork}`\n\nSource: [https://patchwork.data-imaginist.com/articles/patchwork.html](https://patchwork.data-imaginist.com/articles/patchwork.html).\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\nlibrary(patchwork)\n\np2 <- p2 +\n scale_color_manual(values = colors) +\n theme(legend.position = \"none\")\n\np3 <- p3 +\n geom_col(aes(fill = species), width = 0.5) +\n scale_fill_manual(values = colors, name = NULL)\n\npp <- ((p1 / p3) | p2) +\n plot_layout(\n widths = c(1, 2),\n guides = \"collect\"\n ) +\n plot_annotation(\n title = \"Some ggplot2 objects\",\n subtitle = \"The plot (a) shows one aspect, while (b) presents additional data.\",\n tag_levels = \"a\",\n tag_prefix = \"(\",\n tag_suffix = \")\"\n )\n\npp\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-15-1.svg){fig-align='center'}\n:::\n:::\n", + "engine": "knitr", + "markdown": "---\ntitle: \"My favorite `ggplot2` packages with examples\"\nsubtitle: I have to put things somewhere so I don't forget them\ndate: 2023-07-23\ndate-modified: \"2024-03-28\"\ncategories: [minipost, ggplot2, dont-forget]\nimage: images/preview.png \neditor_options: \n chunk_output_type: console\n---\n\n\n\n\nThe idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.\n\n* Typography: `{showtext}`.\n* Annotations: `{ggrepel}`, `{gghighlight}`, `{ggforce}`, `{ggfittext}`.\n* Extra features: `{scales}` and -again- `{ggforce}`.\n* Tree-objects: `{ggparty}` and `{parttree}`.\n\nWhere do we start from? Define some data and some defaults plots that we would have and would like to modify. \n\n\n::: {.cell .column-page layout-ncol=\"3\"}\n\n```{.r .cell-code}\nlibrary(tidyverse)\nlibrary(palmerpenguins)\n\nset.seed(123)\n\nmtcars <- mtcars |>\n rownames_to_column(var = \"name\") |> \n as_tibble() |> \n sample_n(20)\n\npenguins <- penguins |> \n filter(complete.cases(penguins)) \n\np1 <- ggplot(mtcars, aes(wt, mpg, label = name)) +\n geom_point(color = \"cyan4\", size = 2)\np1\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-1.svg)\n:::\n\n```{.r .cell-code}\np2 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +\n geom_point(aes(color = species), size = 2)\np2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-2.svg)\n:::\n\n```{.r .cell-code}\np3 <- penguins |> \n count(species) |> \n ggplot(aes(species, n)) +\n geom_col()\np3\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-3.svg)\n:::\n:::\n\n\nIn my particuar case I like `theme_minimal` theme and legend at the bottom.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntheme_set(\n theme_minimal() +\n theme(\n plot.title = element_text(face = \"plain\"),\n plot.background = element_rect(fill = \"#fcfcfc\", colour = NA),\n panel.background = element_rect(fill = \"#fcfcfc\", colour = NA),\n legend.position = \"bottom\",\n legend.key.width = unit(1.5, \"cm\")\n )\n)\n\ncolors <- c(\"darkorange\",\"darkorchid\",\"cyan4\")\n```\n:::\n\n\nWe're ready to go!\n\n\n## `{showtext}`\n\nSource:[ https://github.com/yixuan/showtext](https://github.com/yixuan/showtext).\n\nThis package makes easy to use typography! Do you want some typography from https://fonts.google.com/? You want it? You got it!\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(showtext)\n\nfont_add_google(\"IBM Plex Sans\", family = \"ibm\")\n\nshowtext_auto()\n\np2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.svg)\n:::\n:::\n\n\n\n## `{ggrepel}`\n\nSource: [https://ggrepel.slowkow.com/articles/examples.html](https://ggrepel.slowkow.com/articles/examples.html).\n\nThis package has been on CRAN/github for a while now. Package that in combination with the `data` argument can make simple and effective annotations.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggrepel)\n\np_repel1 <- p1 +\n geom_text_repel(color = \"gray40\", family = \"ibm\")\n\np_repel1\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-1.svg)\n:::\n\n```{.r .cell-code}\np_repel2 <- p1 +\n geom_text_repel(\n data = ~ filter(.x, mpg > 30), \n color = \"gray40\", \n family = \"ibm\",\n force = 20\n )\n\np_repel2\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-4-2.svg)\n:::\n:::\n\n\n\n## `{gghighlight}`\n\nSource: [https://yutannihilation.github.io/gghighlight/articles/gghighlight.html](https://yutannihilation.github.io/gghighlight/articles/gghighlight.html).\n\n- This package is plug and play if you have to highlight data with a specific filter. \n- Is important to know this package don't work like the usual `geom_*` + `aes()`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(gghighlight) \n\np1 +\n gghighlight(\n # interest subset\n mpg > 30,\n # additional parameters\n label_key = name,\n keep_scales = TRUE,\n label_params = list(color = \"gray40\", label.r = 0, fill = \"gray95\"),\n # can change previous parameters\n unhighlighted_params = list(size = 1.5, color = \"darkorchid\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-5-1.svg)\n:::\n:::\n\n\n\n## `{ggforce}`\n\nSource: [https://ggforce.data-imaginist.com/](https://ggforce.data-imaginist.com/).\n\n- `{ggforce}` takes annotations to the next level.\n- A lot of other functionalities like new geoms, new facets!\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(ggforce)\n\np1 +\n geom_mark_hull(\n aes(filter = mpg > 30, label = \"Interesting\"),\n description = \"Lightweight vehicles have high performance.\",\n color = \"gray70\",\n fill = \"gray90\",\n concavity = 5,\n # control width text\n label.minwidth = unit(100, \"mm\"),\n # how much distance before show legend\n label.buffer = unit(2.5, \"mm\"),\n label.colour = \"gray30\",\n label.family = \"ibm\"\n ) +\n # use circle for points\n geom_mark_circle(\n aes(filter = wt > 3.75, label = NULL, description = name),\n color = \"gray70\",\n fill = \"transparent\",\n label.fontsize = 10,\n label.family = \"ibm\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-6-1.svg)\n:::\n:::\n\n\nAnd other `geom_mark_*` like hull, circle, ellpse, rect.\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\np2 + \n geom_mark_ellipse(\n aes(fill = species, label = species),\n alpha = 0.1,\n color = \"transparent\", # a nice touch sometimes! (imho)\n label.colour = \"gray30\",\n label.family = \"ibm\",\n label.fontsize = 8,\n # label.fontface = \"plain\",\n # this is just for blogpost\n expand = unit(-5, \"mm\"),\n radius = unit(5, \"mm\")\n ) +\n geom_mark_circle(\n aes(\n filter = coalesce(bill_length_mm, 0) == max(bill_length_mm, na.rm = TRUE),\n label = NULL, \n description = \"A rare penguin!\"\n ),\n color = \"gray70\",\n fill = \"transparent\",\n label.fontsize = 8,\n label.family = \"ibm\"\n ) +\n theme(legend.position = \"none\") +\n labs(x = NULL, y = NULL)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-7-1.svg){fig-align='center'}\n:::\n:::\n\n\nNow imagine we used a $K$-means algorithm:\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\ndcenters <- penguins |>\n select(species, bill_length_mm, bill_depth_mm) |> \n filter(!is.na(bill_length_mm)) |> \n filter(!is.na(bill_depth_mm)) |> \n group_by(species) |> \n summarise(across(everything(), median)) |> \n select(-species) |> \n mutate(cluster = as.character(row_number()))\n\ndcenters\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 3 × 3\n bill_length_mm bill_depth_mm cluster\n \n1 38.8 18.4 1 \n2 49.6 18.4 2 \n3 47.4 15 3 \n```\n\n\n:::\n\n```{.r .cell-code}\nbnd <- penguins |>\n summarise(\n min(bill_length_mm, na.rm = TRUE) - 1,\n max(bill_length_mm, na.rm = TRUE) + 1,\n min(bill_depth_mm, na.rm = TRUE) - 1,\n max(bill_depth_mm, na.rm = TRUE) + 1\n ) |> \n as.list() |> \n unlist() |> \n as.vector()\n\np2 +\n geom_voronoi_tile(\n aes(fill = cluster, group = -1),\n data = dcenters, alpha = 0.2, bound = bnd\n ) +\n geom_voronoi_segment(\n aes(group = -1),\n data = dcenters, color = \"gray90\", bound = bnd\n ) +\n xlim(bnd[1], bnd[2]) + \n ylim(bnd[3], bnd[4]) +\n scale_fill_viridis_d(direction = -1, option = \"C\") +\n # its better put point over all layers\n geom_point(\n aes(color = species), \n size = 2,\n shape = 21, \n color = \"gray90\"\n ) +\n theme(legend.position = \"right\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-8-1.svg){fig-align='center'}\n:::\n:::\n\n\n## `{ggfittext}`\n\nSource: [https://github.com/wilkox/ggfittext](https://github.com/wilkox/ggfittext#bar-plots).\n\nThis package have a lot of features to work with strings in ggplot objects, particulary\nwhen you use treemaps.\n\nA love the simple and useful function `geom_bar_text()` + `geom_col()` combo.\n\n\n\n::: {.cell .column-page layout-ncol=\"3\"}\n\n```{.r .cell-code}\nlibrary(ggfittext)\n\np3 <- penguins |> \n count(species) |> \n mutate(n = 100 * n) |> \n ggplot(aes(species, n)) +\n geom_col(width = 0.35, fill = \"cyan4\") \n\np3 +\n geom_bar_text()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-1.svg)\n:::\n\n```{.r .cell-code}\np3 +\n geom_bar_text(\n formatter = scales::comma_format(), \n padding.y = grid::unit(2.5, \"mm\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-2.svg)\n:::\n\n```{.r .cell-code}\np3 + \n geom_bar_text(\n formatter = scales::comma_format(), \n place = \"bottom\",\n padding.y = grid::unit(2.5, \"mm\")\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-9-3.svg)\n:::\n:::\n\n\n\n## `{scales}`\n\nSource: [https://scales.r-lib.org/](https://scales.r-lib.org/).\n\n\nYou use this package using a `label_*` function in the `labels` argument of\n`scale_*` function.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(scales) # comma dollar label_comma\n\nx <- c(123, 456678, 100000)\n\nc(comma(x), dollar(x))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"123\" \"456,678\" \"100,000\" \"$123\" \"$456,678\" \"$100,000\"\n```\n\n\n:::\n\n```{.r .cell-code}\n# I know the correct alternative is Mpg\nmiles_per_gallon <- label_comma(suffix = \" mi/gal\")\n\nwt_lbl <- label_comma(scale = 1000, suffix = \" lbs\")\n\np1 +\n scale_y_continuous(\n labels = miles_per_gallon, \n name = \"fuel consumption\"\n ) +\n scale_x_continuous(\n labels = wt_lbl, \n name = \"weigth\"\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-10-1.svg)\n:::\n:::\n\n\n## `{ggparty}`\n\nSource: [https://github.com/martin-borkovec/ggparty](https://github.com/martin-borkovec/ggparty).\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(partykit) # ctree\nlibrary(ggparty)\n\npenguinct <- ctree(\n species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g + sex,\n data = penguins\n )\n\nautoplot(penguinct)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-11-1.svg)\n:::\n:::\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\nggparty(penguinct) +\n geom_edge(color = \"gray80\") +\n geom_edge_label(color = \"gray50\", size = 4) +\n geom_node_label(\n aes(label = splitvar),\n color = \"gray30\",\n label.col = NA, # no box\n size = 4,\n label.padding = unit(0.5, \"lines\"),\n ids = \"inner\"\n ) +\n geom_node_plot(\n gglist = list(\n geom_point(\n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 1, alpha = 0.5\n ),\n scale_color_viridis_d(end = 0.9),\n guides(color = guide_legend(override.aes = list(size = 5))), \n theme_minimal(),\n theme(axis.text = element_text(size = 7)),\n labs(x = NULL, y = NULL)\n ),\n scales = \"fixed\",\n id = \"terminal\"\n ) +\n geom_node_label(\n aes(label = sprintf(\"Node %s (n = %s)\", id, nodesize)),\n ids = \"terminal\",\n size = 3,\n label.col = NA, # no box\n nudge_y = 0.01\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-12-1.svg){fig-align='center'}\n:::\n:::\n\n\n\n## `{parttree}`\n\nSource: [https://github.com/grantmcdermott/parttree](https://github.com/grantmcdermott/parttree).\n\nI use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# remotes::install_github(\"grantmcdermott/parttree\")\nlibrary(parttree)\n\n# 2 independent variables\npenguinct2 <- ctree(\n species ~ bill_length_mm + bill_depth_mm,\n data = penguins,\n control = ctree_control(maxdepth = 3)\n )\n\nggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +\n geom_parttree(\n data = penguinct2, \n aes(fill = species),\n alpha = 0.2, \n color = \"gray60\",\n ) + \n geom_point(aes(col = species)) +\n theme_minimal()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-13-1.svg)\n:::\n:::\n\n\n## `{ggparty}` + `{parttree}` \n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\ndpred_node <- penguins |>\n select(species, bill_length_mm, bill_depth_mm) |>\n mutate(\n id = predict(penguinct2, type = \"node\", newdata = penguins),\n species_pred = predict(penguinct2, newdata = penguins)\n ) |>\n group_by(id) |>\n summarise(\n species = unique(species_pred),\n bill_length_mm = mean(bill_length_mm),\n bill_depth_mm = mean(bill_depth_mm)\n )\n\ndpred_node\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 5 × 4\n id species bill_length_mm bill_depth_mm\n \n1 3 Adelie 37.4 15.2\n2 4 Adelie 38.5 18.4\n3 6 Gentoo 47.4 14.9\n4 8 Adelie 43.0 18.2\n5 9 Chinstrap 49.5 18.4\n```\n\n\n:::\n\n```{.r .cell-code}\ndparttree <- parttree(penguinct2)\ndparttree <- dparttree |> \n as_tibble() |> \n rename(id = node) |> \n select(-path)\n\nggp <- ggparty(penguinct2) \n\nggp$data <- ggp$data |> \n as_tibble() |> \n left_join(\n dpred_node |> select(id, species),\n by = join_by(id)\n )\n\nggp +\n geom_edge(color = \"gray80\") +\n geom_edge_label(color = \"gray50\", size = 4) +\n geom_node_label(\n aes(label = str_replace_all(splitvar, \"_\", \" \")),\n color = \"gray30\",\n label.col = NA, # no box\n size = 4,\n label.padding = unit(0.5, \"lines\"),\n ids = \"inner\"\n ) +\n geom_node_plot(\n gglist = list(\n geom_point(\n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 1, alpha = 0.5\n ),\n geom_parttree(\n data = penguinct2, \n aes(fill = species),\n alpha = 0.1, \n color = \"gray60\",\n ),\n \n geom_point(\n data = dpred_node, \n aes(x = bill_length_mm, y = bill_depth_mm, color = species),\n size = 3\n ),\n \n geom_rect(\n data = dparttree, \n aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = species),\n alpha = 0.5, \n color = \"gray40\",\n ),\n \n scale_fill_manual(values = colors),\n scale_color_manual(values = colors),\n # scale_color_viridis_d(end = 0.9),\n # scale_fill_viridis_d(end = 0.9),\n guides(color = guide_legend(override.aes = list(size=5))),\n theme_minimal(),\n theme(axis.text = element_text(size = 7)),\n labs(x = NULL, y = NULL)\n ),\n scales = \"fixed\",\n # id = \"all\"\n id = \"terminal\"\n ) +\n geom_node_label(\n aes(label = str_glue(\"{species} (#{id}, n = {nodesize})\")),\n ids = \"terminal\",\n size = 3,\n family = \"ibm\",\n label.col = NA, # no box\n nudge_y = 0.01\n )\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-14-1.svg){fig-align='center'}\n:::\n:::\n\n\n## `{patchwork}`\n\nSource: [https://patchwork.data-imaginist.com/articles/patchwork.html](https://patchwork.data-imaginist.com/articles/patchwork.html).\n\n\n::: {.cell .column-screen-inset layout-align=\"center\"}\n\n```{.r .cell-code}\nlibrary(patchwork)\n\np2 <- p2 +\n scale_color_manual(values = colors) +\n theme(legend.position = \"none\")\n\np3 <- p3 +\n geom_col(aes(fill = species), width = 0.5) +\n scale_fill_manual(values = colors, name = NULL)\n\npp <- ((p1 / p3) | p2) +\n plot_layout(\n widths = c(1, 2),\n guides = \"collect\"\n ) +\n plot_annotation(\n title = \"Some ggplot2 objects\",\n subtitle = \"The plot (a) shows one aspect, while (b) presents additional data.\",\n tag_levels = \"a\",\n tag_prefix = \"(\",\n tag_suffix = \")\"\n )\n\npp\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-15-1.svg){fig-align='center'}\n:::\n:::\n", "supporting": [ "index_files" ], diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-1.svg index c8dbce1..541e306 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-1.svg @@ -3,247 +3,247 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - + + + + + + + + + + + + + - + - + - + - + - - + + - - - + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-2.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-2.svg index 7fde2af..e28473a 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-2.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-2.svg @@ -3,684 +3,684 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - - - + + + + + + - - + + - - + + - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - + - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-3.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-3.svg index 6faeefe..18ca3ba 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-3.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-1-3.svg @@ -3,223 +3,223 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + + + - + - - + + - - - + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + - - - - - - - + + + + + + + - + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-10-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-10-1.svg index bee25cf..c350bce 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-10-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-10-1.svg @@ -2,249 +2,249 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-11-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-11-1.svg index 09003b2..9242082 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-11-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-11-1.svg @@ -5,10 +5,10 @@ - + - + @@ -23,7 +23,7 @@ - + @@ -35,16 +35,16 @@ - + - + - + @@ -59,7 +59,7 @@ - + @@ -71,16 +71,16 @@ - + - + - + @@ -95,7 +95,7 @@ - + @@ -107,16 +107,16 @@ - + - + - + @@ -131,7 +131,7 @@ - + @@ -143,52 +143,52 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -203,7 +203,7 @@ - + @@ -215,16 +215,16 @@ - + - + - + @@ -239,7 +239,7 @@ - + @@ -251,10 +251,10 @@ - + - + @@ -266,599 +266,568 @@ - + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - + + + + + - - - - - + + + + + - - + + - - + + - - - + + + - - - + + + - - - - - + + + + + - - - - - - - + + + + + + + - + - - - + + + - + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + - - - - - + + + + + - - + + - - - - - - - - - - - + + + + + + + + + + + - + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - + + + + + - - + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-12-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-12-1.svg index c700732..29beb20 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-12-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-12-1.svg @@ -41,16 +41,16 @@ - + - + - + - + @@ -62,13 +62,13 @@ - + - + - + @@ -158,16 +158,16 @@ - + - + - + - + @@ -179,13 +179,13 @@ - + - + - + @@ -197,16 +197,16 @@ - + - + - + - + @@ -218,13 +218,13 @@ - + - + - + @@ -275,7 +275,7 @@ - + @@ -285,171 +285,171 @@ - + - - - - - - + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - - - - - - - + + + + + + + + + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - + + - + - + @@ -467,10 +467,10 @@ - + - + @@ -484,23 +484,23 @@ - - + + - + - - + + - - - - - - + + + + + + - + @@ -508,139 +508,139 @@ - - + + - + - + - + - + - + - + - + - + - - + + - + - - + + - - + + - - + + - + - + - + - + - + - - + + - + - - + + - + - + - - + + - - - + + + - - - - - - - + + + + + + + - - - + + + - - - - - - - + + + + + + + - + - + @@ -652,16 +652,16 @@ - + - + - + @@ -679,36 +679,36 @@ - - + + - + - - - - - - + + + + + + - - - + + + - - - - - - - + + + + + + + - + - + @@ -726,10 +726,10 @@ - + - + @@ -743,37 +743,37 @@ - - - - + + + + - - + + - - - - - - + + + + + + - - - + + + - - - - - - - + + + + + + + - + - + @@ -791,16 +791,16 @@ - + - + - + @@ -809,77 +809,77 @@ - + - - - + + + - + - + - - - - + + + + - - + + - - - - - + + + + + - - + + - - - - + + + + - + - + - - - - - - - + + + + + + + - - - + + + - - - - - - - + + + + + + + - + - + - + - + @@ -891,13 +891,13 @@ - + - + - + @@ -906,52 +906,52 @@ - + - - + + - - + + - - - - - - - + + + + + + + - - - + + + - - - - - - - + + + + + + + - + - + - + - + - + @@ -960,13 +960,13 @@ - + - + - + @@ -977,147 +977,147 @@ - + - + - + - - + + - + - + - + - + - - + + - - + + - - + + - - - + + + - + - - - - + + + + - + - - + + - + - - - - + + + + - - + + - + - + - - + + - + - + - + - - + + - + - + - + - + - - + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - + @@ -1132,13 +1132,13 @@ - + - + - + @@ -1156,144 +1156,113 @@ - + - - - - - - - + + + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-13-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-13-1.svg index 865b66e..9c2c735 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-13-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-13-1.svg @@ -2,551 +2,557 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-14-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-14-1.svg index 8f27566..7dfeee9 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-14-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-14-1.svg @@ -74,16 +74,16 @@ - + - + - + - + @@ -95,13 +95,13 @@ - + - + - + @@ -113,13 +113,13 @@ - + - + - + @@ -137,7 +137,7 @@ - + @@ -356,7 +356,7 @@ - + @@ -366,104 +366,104 @@ - - - - - - - - - - + + + + + + + + + + - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - + + + - - - - - - - - - + + + + + + + + + - + - - - - - - - - - + + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - - + + + + + + + @@ -489,7 +489,7 @@ - + @@ -505,12 +505,12 @@ - + - - + + - + @@ -545,35 +545,35 @@ - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - + - + - + - + @@ -585,13 +585,13 @@ - + - + - + @@ -602,144 +602,144 @@ - + - + - + - + - + - - + + - + - - + + - - + + - - - + + + - + - - - + + + - + - + - + - - + + - - - + + + - + - + - + - - - + + + - + - + - - - + + + - - + + - + - + - + - - - + + + - + - - - + + + - - - + + + - - + + - - - - + + + + - - - - - + + + + + - - + + - + - + - + @@ -748,36 +748,36 @@ - + - + - + - + - + - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -803,7 +803,7 @@ - + @@ -812,21 +812,21 @@ - + - - + + - + - + - + @@ -835,25 +835,25 @@ - + - - - + + + - - - + + + - - + + - + - + @@ -861,17 +861,17 @@ - - - + + + - + - + @@ -881,13 +881,13 @@ - + - - - + + + - + @@ -902,27 +902,27 @@ - - + + - + - - + + - - - + + + - - + + - + @@ -958,24 +958,24 @@ - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -1001,7 +1001,7 @@ - + @@ -1016,18 +1016,18 @@ - - + + - + - - - + + + - - + + @@ -1063,24 +1063,24 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + @@ -1106,7 +1106,7 @@ - + @@ -1120,100 +1120,100 @@ - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - + + + + + + + + - - - + + + - - + + - + - + - + - + - + - + - + - + - - + + - - - - - + + + + + - - - - + + + + - + - + - + - + - + - + - + - + - + @@ -1228,147 +1228,107 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-15-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-15-1.svg index 4aae00c..29ceac0 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-15-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-15-1.svg @@ -2,872 +2,872 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - - - - - - - + + + + + + + - - - - - - - - - - + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-3-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-3-1.svg index 82d0dfa..082d9e5 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-3-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-3-1.svg @@ -2,501 +2,501 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-1.svg index c6a850a..f3bfdc5 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-1.svg @@ -2,384 +2,384 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-2.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-2.svg index 141c62a..7d281df 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-2.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-4-2.svg @@ -2,211 +2,212 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-5-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-5-1.svg index 834cbd6..26166cd 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-5-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-5-1.svg @@ -2,218 +2,218 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-6-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-6-1.svg index 9708ffe..dedb299 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-6-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-6-1.svg @@ -2,266 +2,272 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-7-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-7-1.svg index fcc6776..da7e56c 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-7-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-7-1.svg @@ -2,494 +2,494 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-8-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-8-1.svg index 183407d..6663954 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-8-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-8-1.svg @@ -2,881 +2,881 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-1.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-1.svg index a1d794f..9beb9e4 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-1.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-1.svg @@ -2,135 +2,135 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-2.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-2.svg index 8e21a52..bd96221 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-2.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-2.svg @@ -2,138 +2,138 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-3.svg b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-3.svg index b2f63c7..5b08049 100644 --- a/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-3.svg +++ b/_freeze/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index/figure-html/unnamed-chunk-9-3.svg @@ -2,138 +2,138 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/_quarto.yml b/_quarto.yml index eea3a1a..49dc5be 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -35,7 +35,7 @@ website: format: html: - theme: cosmo + theme: materia css: styles.css monofont: "Fira Mono" monobackgroundcolor: "#f7f7f7" diff --git a/docs/index.xml b/docs/index.xml index 3802c30..4307e52 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -15,3273 +15,2182 @@ My favorite ggplot2 packages with examples Joshua Kunst Fuentes https://jkunst.com/blog/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/ - The idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.

+ The idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.

Where do we start from? Define some data and some defaults plots that we would have and would like to modify.

-
-
Code
library(tidyverse)
-library(
+Code
+
palmerpenguinslibrary(tidyverse)
+)
-
-library(palmerpenguins)
+
+set.seedset.seed((123)
+
+mtcars 123<- mtcars )
-
-|>
+  mtcars rownames_to_column(<- var = mtcars "name") |>
-  |> 
+  rownames_to_columnas_tibble() (var |> 
+  = sample_n("name"20)
+
+penguins ) <- penguins |> 
-    as_tibblefilter((complete.cases(penguins)) 
+
+p1 ) <- |> 
-  ggplot(mtcars, sample_naes(wt, mpg, (label = name)) 20+
+  )
-
-geom_point(penguins color = <- "cyan4", penguins size = |> 
-  2)
+p1
+p2 filter<- (ggplot(penguins, complete.casesaes((x = bill_length_mm, penguinsy = bill_depth_mm)) )+
+  ) 
-
-geom_point(p1 aes(<- color = species), ggplotsize = (2)
+p2
+p3 mtcars, <- penguins aes|> 
+  (count(species) wt, |> 
+  mpg, label ggplot(= aes(species, n)) name+
+  )geom_col()
+p3
+
+
+
+
+

+
+
+

+
+
+

+
+
+
+

In my particuar case I like theme_minimal theme and legend at the bottom.

+
+
+Code +
) theme_set(
+  theme_minimal() +
-      geom_pointtheme(
+      (color plot.title = = element_text("cyan4", size face = = "plain"),
+      2plot.background = )
-element_rect(p1
-fill = p2 "#fcfcfc", <- colour = ggplotNA),
+      (panel.background = penguins, element_rect(aesfill = (x "#fcfcfc", = colour = bill_length_mm, y NA),
+      = legend.position = bill_depth_mm"bottom",
+      )legend.key.width = ) unit(+
-  1.5, geom_point"cm")
+    )
+)
+
+colors (<- aesc((color "darkorange",= "darkorchid",species"cyan4")
+
+
+

We’re ready to go!

+
+

{showtext}

+

Source:https://github.com/yixuan/showtext.

+

This package makes easy to use typography! Do you want some typography from https://fonts.google.com/? You want it? You got it!

+
+
+Code +
), size library(showtext)
+
+= font_add_google(2"IBM Plex Sans", )
-family = p2
-"ibm")
+
+p3 showtext_auto()
+
+p2
+
+
+
+
+

+
+
+
+
+
+
+

{ggrepel}

+

Source: https://ggrepel.slowkow.com/articles/examples.html.

+

This package has been on CRAN/github for a while now. Package that in combination with the data argument can make simple and effective annotations.

+
+
+Code +
<- library(ggrepel)
+
+p_repel1 penguins <- p1  |> 
-  +
+  countgeom_text_repel((color = species"gray40", ) family  = |> 
-  "ibm")
+
+p_repel1
+
+
+
+
+

+
+
+
+
+Code +
p_repel2 ggplot<- p1 (+
+  aesgeom_text_repel(
+    (data = species, ~ nfilter(.x, mpg )> ) 30), 
+    +
-  color = geom_col"gray40", 
+    (family  = )
-"ibm",
+     p3
-
-
-
-

-
-
-

-
-
-

-
+font-style: inherit;">force = 20 + ) + +p_repel2
+ +
+
+
+

+
-

In my particuar case I like theme_minimal theme and legend at the bottom.

+
+
+

{gghighlight}

+

Source: https://yutannihilation.github.io/gghighlight/articles/gghighlight.html.

+
    +
  • This package is plug and play if you have to highlight data with a specific filter.
  • +
  • Is important to know this package don’t work like the usual geom_* + aes().
  • +
-
Code

+Code
+
theme_setlibrary(gghighlight) 
+
+p1 (
-  +
+  theme_minimalgghighlight(
+    (# interest subset
+    mpg ) > +
-    30,
+    theme# additional parameters
+    (
-      plot.title label_key = name,
+    = keep_scales = element_textTRUE,
+    (face label_params = = list("plain"color = ),
-      plot.background "gray40", = label.r = element_rect(fill = "#fcfcfc", colour = NA),
-      panel.background = element_rect(fill = "#fcfcfc", colour = NA),
-      legend.position = "bottom",
-      legend.key.width = unit(1.5, "cm")
-    )
-0, )
-
-fill = colors "gray95"),
+    <- # can change previous parameters
+    cunhighlighted_params = (list("darkorange",size = "darkorchid",1.5, "cyan4"color = )
+font-style: inherit;">"darkorchid"
)
+ )
+
+
+
+

+
-

We’re ready to go!

-

{showtext}

-

Source:https://github.com/yixuan/showtext.

-

This package makes easy to use typography! Do you want some typography from https://fonts.google.com/? You want it? You got it!

-
-
Code
library(showtext)
-
-font_add_google("IBM Plex Sans", family = "ibm")
-
-showtext_auto()
-
-p2
-
-

-
-
-

{ggrepel}

-

Source: https://ggrepel.slowkow.com/articles/examples.html.

-

This package has been on CRAN/github for a while now. Package that in combination with the data argument can make simple and effective annotations.

-
-
Code
library(ggrepel)
-
-p_repel1 <- p1  +
-  geom_text_repel(color = "gray40", family  = "ibm")
-
-p_repel1
-
-

-
-
Code
p_repel2 <- p1 +
-  geom_text_repel(
-    data = ~ filter(.x, mpg > 30), 
-    color = "gray40", 
-    family  = "ibm",
-     force = 20
-    )
-
-p_repel2
-
-

-

{gghighlight}

-

Source: https://yutannihilation.github.io/gghighlight/articles/gghighlight.html.

+
+
+

{ggforce}

+

Source: https://ggforce.data-imaginist.com/.

    -
  • This package is plug and play if you have to highlight data with a specific filter.
  • -
  • Is important to know this package don’t work like the usual geom_* + aes().
  • +
  • {ggforce} takes annotations to the next level.
  • +
  • A lot of other functionalities like new geoms, new facets!
-
Code
library(gghighlight) 
-
-
+Code
+
p1 library(ggforce)
+
+p1 +
-  gghighlight(
-    # interest subset
-    mpg > 30,
-    # additional parameters
-    label_key = name,
-    keep_scales = TRUE,
-    label_params = list(color   = geom_mark_hull(
+    "gray40", label.r aes(= filter =  mpg 0, fill > = 30, "gray95"label = ),
-    "Interesting"),
+    # can change previous parameters
-    unhighlighted_params description = = "Lightweight vehicles have high performance.",
+    listcolor = (size "gray70",
+    = fill = 1.5, color "gray90",
+    = concavity = "darkorchid")
-    )
-
-

-
-
-

{ggforce}

-

Source: https://ggforce.data-imaginist.com/.

-
    -
  • -ggforce takes annotations to the next level.
  • -
  • A lot of other functionalities like new geoms, new facets!
  • -
-
-
Code
library(ggforce)
-
-p1 +
-  geom_mark_hull(
-    aes(filter =  mpg > 30, label = "Interesting"),
-    description = "Lightweight vehicles have high performance.",
-    color = "gray70",
-    fill = "gray90",
-    concavity = 5,
-    5,
+    # control width text
-    label.minwidth = unit(100, "mm"),
-    # how much distance before show legend
-    label.buffer = unit(2.5, "mm"),
-    label.colour = "gray30",
-    label.family = "ibm"
-  ) +
-  # use circle for points
-  geom_mark_circle(
-    aes(filter =  wt > 3.75, label = NULL, description = name),
-    color = "gray70",
-    fill = "transparent",
-    label.fontsize = 10,
-    label.family = "ibm"
-   )
-
-

-
-
-

And other geom_mark_* like hull, circle, ellpse, rect.

-
-
Code
p2 + 
-  geom_mark_ellipse(
-    aes(fill = species, label = species),
-    alpha = 0.1,
-    color = "transparent", # a nice touch sometimes! (imho)
-    label.colour = "gray30",
-    label.family = "ibm",
-    label.fontsize = 8,
-    # label.fontface = "plain",
-    # this is just for blogpost
-    expand = unit(-5, "mm"),
-    radius = unit(5, "mm")
-    ) +
-  geom_mark_circle(
-    aes(
-      filter = coalesce(bill_length_mm, 0) == max(bill_length_mm, na.rm = TRUE),
-      label = NULL, 
-      description = "A rare penguin!"
-      ),
-    color = "gray70",
-    fill = "transparent",
-    label.fontsize = 8,
-    label.family = "ibm"
-   ) +
-  theme(legend.position = "none") +
-  labs(x = NULL, y = NULL)
-
-
-

-
-
-
-
-

Now imagine we used a -means algorithm:

-
-
Code
dcenters <- penguins |>
-  select(species, bill_length_mm, bill_depth_mm) |> 
-  filter(!is.na(bill_length_mm)) |> 
-  filter(!is.na(bill_depth_mm)) |> 
-  group_by(species) |> 
-  summarise(across(everything(), median)) |> 
-  select(-species) |> 
-  mutate(cluster = as.character(row_number()))
-
-dcenters
-
-
# A tibble: 3 × 3
-  bill_length_mm bill_depth_mm cluster
-           <dbl>         <dbl> <chr>  
-1           38.8          18.4 1      
-2           49.6          18.4 2      
-3           47.4          15   3      
-
-
Code
bnd <- penguins |>
-  summarise(
-    min(bill_length_mm, na.rm = TRUE) - 1,
-    max(bill_length_mm, na.rm = TRUE) + 1,
-    min(bill_depth_mm, na.rm = TRUE)  - 1,
-    max(bill_depth_mm, na.rm = TRUE)  + 1
-  ) |> 
-  as.list() |> 
-  unlist() |> 
-  as.vector()
-
-p2 +
-  geom_voronoi_tile(
-    aes(fill = cluster, group = -1),
-    data = dcenters, alpha = 0.2, bound = bnd
-    ) +
-  geom_voronoi_segment(
-    aes(group = -1),
-    data = dcenters, color = "gray90", bound = bnd
-    ) +
-  xlim(bnd[1], bnd[2]) + 
-  ylim(bnd[3], bnd[4]) +
-  scale_fill_viridis_d(direction = -1, option = "C") +
-  # its better put point over all layers
-  geom_point(
-    aes(color = species), 
-    size = 2,
-    shape = 21, 
-    color = "gray90"
-    ) +
-  theme(legend.position = "right")
-
-
-

-
-
-
-
-

{ggfittext}

-

Source: https://github.com/wilkox/ggfittext.

-

This package have a lot of features to work with strings in ggplot objects, particulary when you use treemaps.

-

A love the simple and useful function geom_bar_text() + geom_col() combo.

-
-
Code
library(ggfittext)
-
-p3 <- penguins |> 
-  count(species) |> 
-  mutate(n = 100 * n) |> 
-  ggplot(aes(species, n)) +
-  geom_col(width = 0.35, fill = "cyan4") 
-
-p3 +
-  geom_bar_text()
-p3 +
-  geom_bar_text(
-    formatter = scales::comma_format(), 
-    padding.y =  grid::unit(2.5, "mm")
-  )
-p3 + 
-  geom_bar_text(
-    formatter = scales::comma_format(), 
-    place = "bottom",
-    padding.y =  grid::unit(2.5, "mm")
-  )
-
-
-
-

-
-
-

-
-
-

-
-
-
-
-

{scales}

-

Source: https://scales.r-lib.org/.

-

You use this package using a label_* function in the labels argument of scale_* function.

-
-
Code
library(scales) # comma dollar label_comma
-
-x <- c(123, 456678, 100000)
-
-c(comma(x), dollar(x))
-
-
[1] "123"      "456,678"  "100,000"  "$123"     "$456,678" "$100,000"
-
-
Code
# I know the correct alternative is Mpg
-miles_per_gallon <- label_comma(suffix = " mi/gal")
-
-wt_lbl <- label_comma(scale = 1000, suffix = " lbs")
-
-p1 +
-  scale_y_continuous(
-    labels = miles_per_gallon, 
-    name = "fuel consumption"
-    ) +
-  scale_x_continuous(
-    labels = wt_lbl, 
-    name = "weigth"
-    )
-
-

-
-
-

{ggparty}

-

Source: https://github.com/martin-borkovec/ggparty.

-
-
Code
library(partykit) # ctree
-    librarylabel.minwidth = (unit(ggparty100, )
-
-"mm"),
+    penguinct # how much distance before show legend
+    <- label.buffer = ctreeunit((
-  2.5, species "mm"),
+    ~ label.colour = bill_length_mm "gray30",
+    + label.family = bill_depth_mm "ibm"
+  ) + +
+  flipper_length_mm # use circle for points
+  + geom_mark_circle(
+    body_mass_g aes(+ filter =  wt sex,
-  data > = 3.75, penguins
-  label = )
-
-NULL, autoplotdescription = name),
+    (color = penguinct"gray70",
+    )
-
-

-
-
-
-
Code
fill = ggparty"transparent",
+    (label.fontsize = penguinct10,
+    ) label.family = +
-  "ibm"
+   )
+
+
+
+
+

+
+
+
+
+

And other geom_mark_* like hull, circle, ellpse, rect.

+
+
+Code +
p2 geom_edge+ 
+  (color geom_mark_ellipse(
+    = aes("gray80"fill = species, ) label = species),
+    +
-  alpha = geom_edge_label0.1,
+    (color color = = "transparent", "gray50", size # a nice touch sometimes! (imho)
+    = label.colour = 4"gray30",
+    ) label.family = +
-  "ibm",
+    geom_node_labellabel.fontsize = (
-    8,
+    aes# label.fontface = "plain",
+    (label # this is just for blogpost
+    = expand = splitvarunit(),
-    color -= 5, "gray30",
-    label.col "mm"),
+    = radius = NA, unit(# no box
-    size 5, = "mm")
+    ) 4,
-    label.padding +
+  = geom_mark_circle(
+    unitaes(
+      (filter = 0.5, coalesce(bill_length_mm, "lines"0) ),
-    ids == = max(bill_length_mm, "inner"
-    na.rm = ) TRUE),
+      +
-  label = geom_node_plotNULL, 
+      (
-    gglist description = = "A rare penguin!"
+      ),
+    listcolor = (
-      "gray70",
+    geom_pointfill = (
-        "transparent",
+    aeslabel.fontsize = (x 8,
+    = label.family = bill_length_mm, y "ibm"
+   ) = +
+  bill_depth_mm, color theme(= legend.position = species"none") ),
-        size +
+  = labs(1, alpha x = = NULL, 0.5
-        y = ),
-      NULL)
+
+
+
+
+

+
+
+
+
+

Now imagine we used a -means algorithm:

+
+
+Code +
dcenters scale_color_viridis_d<- penguins (end |>
+  = select(species, bill_length_mm, bill_depth_mm) 0.9|> 
+  ),
-      filter(guides!(color is.na(bill_length_mm)) = |> 
+  guide_legendfilter((override.aes != is.na(bill_depth_mm)) list|> 
+  (size group_by(species) = |> 
+  5summarise()across()everything(), median)) ), 
-      |> 
+  theme_minimalselect((-species) ),
-      |> 
+  thememutate((axis.text cluster = = as.character(element_textrow_number()))
+
+dcenters
+
+
+
# A tibble: 3 × 3
+  bill_length_mm bill_depth_mm cluster
+           <dbl>         <dbl> <chr>  
+1           38.8          18.4 1      
+2           49.6          18.4 2      
+3           47.4          15   3      
+
+
+Code +
bnd (size <- penguins = |>
+  7summarise(
+    )min(bill_length_mm, ),
-      na.rm = labsTRUE) (x - = 1,
+    NULL, y max(bill_length_mm, = na.rm = NULLTRUE) )
-    + ),
-    scales 1,
+    = min(bill_depth_mm, "fixed",
-    id na.rm = = TRUE)  "terminal"
-  - ) 1,
+    +
-  max(bill_depth_mm, geom_node_labelna.rm = (
-    TRUE)  aes+ (label 1
+  ) = |> 
+  sprintfas.list() (|> 
+  "Node %s (n = %s)", unlist() id, |> 
+  nodesizeas.vector()
+
+p2 )+
+  ),
-    ids geom_voronoi_tile(
+    = aes("terminal",
-    size fill = cluster, = group = 3,
-    label.col -= 1),
+    NA, data = dcenters, # no box
-    nudge_y alpha = = 0.2, 0.01
-    bound = bnd
+    ) )
-
-
-

-
-
-
-
-

{parttree}

-

Source: https://github.com/grantmcdermott/parttree.

-

I use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.

-
-
Code
+
+  # remotes::install_github("grantmcdermott/parttree")
-geom_voronoi_segment(
+    libraryaes((group = parttree-)
-
-1),
+    # 2 independent variables
-data = dcenters, penguinct2 color = <- "gray90", ctreebound = bnd
+    ) (
-  +
+  species xlim(bnd[~ 1], bnd[bill_length_mm 2]) + + 
+  bill_depth_mm,
-  data ylim(bnd[= 3], bnd[penguins,
-  control 4]) = +
+  ctree_controlscale_fill_viridis_d((maxdepth direction = = -31, )
-  option = )
-
-"C") ggplot+
+  (# its better put point over all layers
+  penguins, geom_point(
+    aesaes((x color = species), 
+    = size = bill_length_mm, y 2,
+    = shape = bill_depth_mm21, 
+    )color = ) "gray90"
+    ) +
-    geom_parttreetheme((
-    data legend.position = = "right")
+
+
+
+
+

+
+
+
+
+
+
+

{ggfittext}

+

Source: https://github.com/wilkox/ggfittext.

+

This package have a lot of features to work with strings in ggplot objects, particulary when you use treemaps.

+

A love the simple and useful function geom_bar_text() + geom_col() combo.

+
+Code +
penguinct2, 
-    library(ggfittext)
+
+p3 aes<- penguins (fill |> 
+  = count(species) species|> 
+  ),
-    alpha mutate(= n = 0.2, 
-    color 100 = * n) "gray60",
-    |> 
+  ) ggplot(+ 
-  aes(species, n)) geom_point+
+  (geom_col(aeswidth = (col 0.35, = fill = species"cyan4") 
+
+p3 )+
+  ) geom_bar_text()
+p3 +
-  theme_minimal  (geom_bar_text(
+    )
-
-

-
-
-

-{ggparty} + {parttree} -

-
-
Code
formatter = scalesdpred_node ::<- comma_format(), 
+    penguins padding.y =  grid|>
-  ::selectunit((2.5, species, "mm")
+  )
+p3 bill_length_mm, + 
+  bill_depth_mmgeom_bar_text(
+    ) formatter = scales|>
-  ::mutatecomma_format(), 
+    (
-    id place = = "bottom",
+    predictpadding.y =  grid(::penguinct2, type unit(= 2.5, "node", newdata "mm")
+  )
+
+
+
+
+

+
+
+

+
+
+

+
+
+
+
+
+

{scales}

+

Source: https://scales.r-lib.org/.

+

You use this package using a label_* function in the labels argument of scale_* function.

+
+
+Code +
= library(scales) penguins# comma dollar label_comma
+
+x ),
-    species_pred <- = c(predict123, (456678, penguinct2, newdata 100000)
+
+= c(penguinscomma(x), )
-    dollar(x))
+
+
+
[1] "123"      "456,678"  "100,000"  "$123"     "$456,678" "$100,000"
+
+
+Code +
) # I know the correct alternative is Mpg
+miles_per_gallon |>
-  <- group_bylabel_comma((suffix = id" mi/gal")
+
+wt_lbl ) <- |>
-  label_comma(summarisescale = (
-    species 1000, = suffix = unique" lbs")
+
+p1 (+
+  species_predscale_y_continuous(
+    ),
-    bill_length_mm labels = miles_per_gallon, 
+    = name = mean"fuel consumption"
+    ) (+
+  bill_length_mmscale_x_continuous(
+    ),
-    bill_depth_mm labels = wt_lbl, 
+    = name = mean"weigth"
+    )
+
+
+
+
+

+
+
+
+
+
+
+

{ggparty}

+

Source: https://github.com/martin-borkovec/ggparty.

+
+
+Code +
(library(partykit) bill_depth_mm# ctree
+)
-  library(ggparty)
+
+penguinct )
-
-<- dpred_node
-
-
# A tibble: 5 × 4
-     id species   bill_length_mm bill_depth_mm
-  <int> <fct>              <dbl>         <dbl>
-1     3 Adelie              37.4          15.2
-2     4 Adelie              38.5          18.4
-3     6 Gentoo              47.4          14.9
-4     8 Adelie              43.0          18.2
-5     9 Chinstrap           49.5          18.4
-
-
Code
ctree(
+  species dparttree ~ bill_length_mm <- + bill_depth_mm parttree+ flipper_length_mm (+ body_mass_g penguinct2+ sex,
+  )
-data = penguins
+  )
+
+dparttree autoplot(penguinct)
+
+
+
+
+

+
+
+
+
+
+
+Code +
<- ggparty(penguinct) dparttree +
+  |> 
-  geom_edge(as_tibblecolor = ("gray80") ) +
+  |> 
-  geom_edge_label(renamecolor = (id "gray50", = size = node4) ) +
+  |> 
-  geom_node_label(
+    selectaes((label = splitvar),
+    -color = path"gray30",
+    )
-
-label.col = ggp NA, <- # no box
+    ggpartysize = (4,
+    penguinct2label.padding = ) 
-
-unit(ggp0.5, $"lines"),
+    data ids = <- "inner"
+    ) ggp+
+  $geom_node_plot(
+    data gglist = |> 
-  list(
+      as_tibblegeom_point(
+        (aes() x = bill_length_mm, |> 
-  y = bill_depth_mm, left_joincolor = species),
+        (
-    size = dpred_node 1, |> alpha = select0.5
+        ),
+      (scale_color_viridis_d(id, end = species0.9),
+      ),
-    by guides(= color = join_byguide_legend((override.aes = idlist()
-    size = )
-
-5))), 
+      ggp theme_minimal(),
+      +
-  theme(geom_edgeaxis.text = (color element_text(= size = "gray80"7)),
+      ) labs(+
-  x = geom_edge_labelNULL, (color y = = NULL)
+    ),
+    "gray50", size scales = = "fixed",
+    4id = ) "terminal"
+  ) +
-    geom_node_labelgeom_node_label(
+    (
-    aes(aeslabel = (label sprintf(= "Node %s (n = %s)", id, nodesize)),
+    str_replace_allids = ("terminal",
+    splitvar, size = "_", 3,
+    " "label.col = )NA, ),
-    color # no box
+    = nudge_y = "gray30",
-    label.col 0.01
+    )
+
+
+
+
+

+
+
+
+
+
+
+

{parttree}

+

Source: https://github.com/grantmcdermott/parttree.

+

I use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.

+
+
+Code +
= # remotes::install_github("grantmcdermott/parttree")
+NA, library(parttree)
+
+# no box
-    size # 2 independent variables
+penguinct2 = <- 4,
-    label.padding ctree(
+  species = ~ bill_length_mm unit+ bill_depth_mm,
+  (data = penguins,
+  0.5, control = "lines"ctree_control(),
-    ids maxdepth = = 3)
+  )
+
+"inner"
-    ggplot(penguins, ) aes(x = bill_length_mm, y = bill_depth_mm)) +
-    geom_node_plotgeom_parttree(
+    (
-    gglist data = penguinct2, 
+    = aes(listfill = species),
+    (
-      alpha = geom_point0.2, 
+    (
-        color = aes"gray60",
+    ) (x + 
+  = geom_point(bill_length_mm, y aes(= col = species)) bill_depth_mm, color +
+  = theme_minimal()
+
+
+
+
+

+
+
+
+
+
+
+

{ggparty} + {parttree}

+
+
+Code +
dpred_node species<- penguins ),
-        size |>
+  = select(species, bill_length_mm, bill_depth_mm) 1, alpha |>
+  = mutate(
+    0.5
-        id = ),
-      predict(penguinct2, type = geom_parttree"node", (
-        data newdata = penguins),
+    = species_pred = penguinct2, 
-        predict(penguinct2, aesnewdata = penguins)
+    ) (fill |>
+  = group_by(id) species|>
+  ),
-        alpha summarise(
+    = species = 0.1, 
-        color unique(species_pred),
+    = bill_length_mm = "gray60",
-        mean(bill_length_mm),
+    ),
-      
-      bill_depth_mm = geom_pointmean(bill_depth_mm)
+  )
+
+dpred_node
+
+
+
# A tibble: 5 × 4
+     id species   bill_length_mm bill_depth_mm
+  <int> <fct>              <dbl>         <dbl>
+1     3 Adelie              37.4          15.2
+2     4 Adelie              38.5          18.4
+3     6 Gentoo              47.4          14.9
+4     8 Adelie              43.0          18.2
+5     9 Chinstrap           49.5          18.4
+
+
+Code +
dparttree (
-        data <- = parttree(penguinct2)
+dparttree dpred_node, 
-        <- dparttree aes|> 
+  (x as_tibble() = |> 
+  bill_length_mm, y rename(= id = node) bill_depth_mm, color |> 
+  = select(species-path)
+
+ggp ),
-        size <- = ggparty(penguinct2) 
+
+ggp3
-        $data ),
-      
-      <- ggpgeom_rect$data (
-        data |> 
+  = as_tibble() dparttree, 
-        |> 
+  aesleft_join(
+    dpred_node (xmin |> = select(id, species),
+    xmin, xmax by = = join_by(id)
+    )
+
+ggp xmax, ymin +
+  = geom_edge(ymin, ymax color = = "gray80") ymax, fill +
+  = geom_edge_label(speciescolor = ),
-        alpha "gray50", = size = 0.5, 
-        color 4) = +
+  "gray40",
-        geom_node_label(
+    ),
-      
-      aes(scale_fill_manuallabel = (values str_replace_all(splitvar, = "_", colors" ")),
+    ),
-      color = scale_color_manual"gray30",
+    (values label.col = = NA, colors# no box
+    ),
-      size = # scale_color_viridis_d(end = 0.9),
-      4,
+    # scale_fill_viridis_d(end = 0.9),
-      label.padding = guidesunit((color 0.5, = "lines"),
+    guide_legendids = (override.aes "inner"
+    ) = +
+  listgeom_node_plot(
+    (sizegglist = =list(
+      5geom_point(
+        )aes()x = bill_length_mm, ),
-      y = bill_depth_mm, theme_minimalcolor = species),
+        (size = ),
-      1, themealpha = (axis.text 0.5
+        ),
+      = geom_parttree(
+        element_textdata = penguinct2, 
+        (size aes(= fill = species),
+        7alpha = )0.1, 
+        ),
-      color = labs"gray60",
+        ),
+      
+      (x geom_point(
+        = data = dpred_node, 
+        NULL, y aes(= x = bill_length_mm, NULLy = bill_depth_mm, )
-    color = species),
+        ),
-    scales size = = 3
+        ),
+      
+      "fixed",
-    geom_rect(
+        # id = "all"
-    id data = dparttree, 
+        = aes("terminal"
-  xmin = xmin, ) xmax = xmax, +
-  ymin = ymin, geom_node_labelymax = ymax, (
-    fill = species),
+        aesalpha = (label 0.5, 
+        = color = str_glue"gray40",
+        ),
+      
+      (scale_fill_manual("{species} (#{id},  n = {nodesize})"values = colors),
+      )scale_color_manual(),
-    ids values = colors),
+      = # scale_color_viridis_d(end = 0.9),
+      "terminal",
-    size # scale_fill_viridis_d(end = 0.9),
+      = guides(3,
-    family color = = guide_legend("ibm",
-    label.col override.aes = = list(NA, size=# no box
-    nudge_y 5))),
+      = theme_minimal(),
+      0.01
-    theme()
-
-
-

-
-
-
-
-

{patchwork}

-

Source: https://patchwork.data-imaginist.com/articles/patchwork.html.

-
-
Code
axis.text = libraryelement_text((size = patchwork7)),
+      )
-
-labs(p2 x = <- NULL, p2 y = +
-  NULL)
+    ),
+    scale_color_manualscales = (values "fixed",
+    = # id = "all"
+    colorsid = ) "terminal"
+  ) +
-  theme  (legend.position geom_node_label(
+    = aes("none"label = )
-
-str_glue(p3 "{species} (#{id},  n = {nodesize})")),
+    <- ids = p3 "terminal",
+    +
-  size = geom_col3,
+    (family = aes"ibm",
+    (fill label.col = = NA, species# no box
+    ), width nudge_y = = 0.01
+    )
+
+
+
+
+

+
+
+
+
+
+
+

{patchwork}

+

Source: https://patchwork.data-imaginist.com/articles/patchwork.html.

+
+
+Code +
0.5library(patchwork)
+
+p2 ) <- p2 +
-    scale_fill_manualscale_color_manual((values values = colors) = +
+  colors, name theme(= legend.position = NULL"none")
+
+p3 )
-
-<- p3 pp +
+  <- geom_col((aes((fill = species), p1 width = / 0.5) p3+
+  ) scale_fill_manual(| values = colors, p2name = ) NULL)
+
+pp +
-  <- ((p1 plot_layout/ p3) (
-    widths | p2) = +
+  cplot_layout(
+    (widths = 1, c(21, ),
-    guides 2),
+    = guides = "collect"
-    )     ) +
-    plot_annotationplot_annotation(
+    (
-    title = title = "Some ggplot2 objects",
-    subtitle     = subtitle = "The plot (a) shows one aspect, while (b) presents additional data.",
-    tag_levels     = tag_levels = "a",
-    tag_prefix     = tag_prefix = "(",
-    tag_suffix     = tag_suffix = ")"
-    )
-
-pp
-
-
-

+ ) + +pp
+ +
+
+
+

-

Reuse

Citation

BibTeX citation:
@online{kunstfuentes2023,
-  author = {Joshua Kunst Fuentes},
+
+ +

Reuse

Citation

BibTeX citation:
@online{kunst fuentes2023,
+  author = {Kunst Fuentes, Joshua},
   title = {My Favorite `Ggplot2` Packages with Examples},
   date = {2023-07-23},
   url = {https://jkunst.com/blog/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples},
   langid = {en}
 }
-
For attribution, please cite this work as:
-Joshua Kunst Fuentes. 2023. “My Favorite `Ggplot2` Packages with +
For attribution, please cite this work as:
+Kunst Fuentes, Joshua. 2023. “My Favorite `Ggplot2` Packages with Examples.” July 23, 2023. https://jkunst.com/blog/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples.
]]> minipost diff --git a/docs/posts/2000-01-01-quarto-post-example/index.html b/docs/posts/2000-01-01-quarto-post-example/index.html index 5c73dad..344790f 100644 --- a/docs/posts/2000-01-01-quarto-post-example/index.html +++ b/docs/posts/2000-01-01-quarto-post-example/index.html @@ -316,8 +316,8 @@

Testing htmlwidgets```

-
- +
+
@@ -467,8 +467,8 @@

Marging content

-
- +
+
diff --git a/docs/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index.html b/docs/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index.html index 7292545..cec7597 100644 --- a/docs/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index.html +++ b/docs/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples/index.html @@ -1,11 +1,14 @@ - + - + + + + Data, Code and Visualization - My favorite ggplot2 packages with examples + @@ -99,13 +74,20 @@ - + + + + + + + + + + - - - + + + - - + + +
-
+
+ +
+

My favorite ggplot2 packages with examples

I have to put things somewhere so I don’t forget them

@@ -200,7 +227,7 @@

My favorite ggplot2 packages with examples

Author
-

Joshua Kunst Fuentes

+

Joshua Kunst Fuentes

@@ -214,7 +241,7 @@

My favorite ggplot2 packages with examples

Modified
-

March 26, 2024

+

March 28, 2024

@@ -223,10 +250,12 @@

My favorite ggplot2 packages with examples

- +
+ + -

The idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.

+
+ + + + + +

The idea is to show some packages and options that I use on a daily basis, and leave code with annotations and defaults that I normally use. We will go through the packages one by one with examples, but these can be grouped into the following categories.

Where do we start from? Define some data and some defaults plots that we would have and would like to modify.

-
-
Code
library(tidyverse)
-library(palmerpenguins)
-
-set.seed(123)
-
-mtcars <- mtcars |>
-  rownames_to_column(var = "name") |> 
-  as_tibble() |> 
-  sample_n(20)
-
-penguins <- penguins |> 
-  filter(complete.cases(penguins)) 
-
-p1 <- ggplot(mtcars, aes(wt, mpg, label = name)) +
-  geom_point(color = "cyan4", size = 2)
-p1
-p2 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
-  geom_point(aes(color = species), size = 2)
-p2
-p3 <- penguins |> 
-  count(species) |> 
-  ggplot(aes(species, n)) +
-  geom_col()
-p3
-
-
-
+
+Code +
library(tidyverse)
+library(palmerpenguins)
+
+set.seed(123)
+
+mtcars <- mtcars |>
+  rownames_to_column(var = "name") |> 
+  as_tibble() |> 
+  sample_n(20)
+
+penguins <- penguins |> 
+  filter(complete.cases(penguins)) 
+
+p1 <- ggplot(mtcars, aes(wt, mpg, label = name)) +
+  geom_point(color = "cyan4", size = 2)
+p1
+p2 <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
+  geom_point(aes(color = species), size = 2)
+p2
+p3 <- penguins |> 
+  count(species) |> 
+  ggplot(aes(species, n)) +
+  geom_col()
+p3
+
+
+
+

-
+

-
+

-

In my particuar case I like theme_minimal theme and legend at the bottom.

-
Code
theme_set(
-  theme_minimal() +
-    theme(
-      plot.title = element_text(face = "plain"),
-      plot.background = element_rect(fill = "#fcfcfc", colour = NA),
-      panel.background = element_rect(fill = "#fcfcfc", colour = NA),
-      legend.position = "bottom",
-      legend.key.width = unit(1.5, "cm")
-    )
-)
-
-colors <- c("darkorange","darkorchid","cyan4")
+
+Code +
theme_set(
+  theme_minimal() +
+    theme(
+      plot.title = element_text(face = "plain"),
+      plot.background = element_rect(fill = "#fcfcfc", colour = NA),
+      panel.background = element_rect(fill = "#fcfcfc", colour = NA),
+      legend.position = "bottom",
+      legend.key.width = unit(1.5, "cm")
+    )
+)
+
+colors <- c("darkorange","darkorchid","cyan4")

We’re ready to go!

-

{showtext}

+
+

{showtext}

Source:https://github.com/yixuan/showtext.

This package makes easy to use typography! Do you want some typography from https://fonts.google.com/? You want it? You got it!

-
Code
library(showtext)
-
-font_add_google("IBM Plex Sans", family = "ibm")
-
-showtext_auto()
-
-p2
-
-

-
-
-

{ggrepel}

+
+Code +
library(showtext)
+
+font_add_google("IBM Plex Sans", family = "ibm")
+
+showtext_auto()
+
+p2
+
+
+
+
+

+
+
+
+
+ +
+

{ggrepel}

Source: https://ggrepel.slowkow.com/articles/examples.html.

This package has been on CRAN/github for a while now. Package that in combination with the data argument can make simple and effective annotations.

-
Code
library(ggrepel)
-
-p_repel1 <- p1  +
-  geom_text_repel(color = "gray40", family  = "ibm")
-
-p_repel1
-
-

-
-
Code
p_repel2 <- p1 +
-  geom_text_repel(
-    data = ~ filter(.x, mpg > 30), 
-    color = "gray40", 
-    family  = "ibm",
-     force = 20
-    )
-
-p_repel2
-
-

-
-
-

{gghighlight}

+
+Code +
library(ggrepel)
+
+p_repel1 <- p1  +
+  geom_text_repel(color = "gray40", family  = "ibm")
+
+p_repel1
+
+
+
+
+

+
+
+
+
+Code +
p_repel2 <- p1 +
+  geom_text_repel(
+    data = ~ filter(.x, mpg > 30), 
+    color = "gray40", 
+    family  = "ibm",
+     force = 20
+    )
+
+p_repel2
+
+
+
+
+

+
+
+
+
+ +
+

{gghighlight}

Source: https://yutannihilation.github.io/gghighlight/articles/gghighlight.html.

  • This package is plug and play if you have to highlight data with a specific filter.
  • -
  • Is important to know this package don’t work like the usual geom_* + aes().
  • +
  • Is important to know this package don’t work like the usual geom_* + aes().
-
Code
library(gghighlight) 
-
-p1 +
-  gghighlight(
-    # interest subset
-    mpg > 30,
-    # additional parameters
-    label_key = name,
-    keep_scales = TRUE,
-    label_params = list(color = "gray40", label.r = 0, fill = "gray95"),
-    # can change previous parameters
-    unhighlighted_params = list(size = 1.5, color = "darkorchid")
-    )
-
-

-
-
-

{ggforce}

+
+Code +
library(gghighlight) 
+
+p1 +
+  gghighlight(
+    # interest subset
+    mpg > 30,
+    # additional parameters
+    label_key = name,
+    keep_scales = TRUE,
+    label_params = list(color = "gray40", label.r = 0, fill = "gray95"),
+    # can change previous parameters
+    unhighlighted_params = list(size = 1.5, color = "darkorchid")
+    )
+
+
+
+
+

+
+
+
+
+ +
+

{ggforce}

Source: https://ggforce.data-imaginist.com/.

    -
  • -ggforce takes annotations to the next level.
  • +
  • {ggforce} takes annotations to the next level.
  • A lot of other functionalities like new geoms, new facets!
-
Code
library(ggforce)
-
-p1 +
-  geom_mark_hull(
-    aes(filter =  mpg > 30, label = "Interesting"),
-    description = "Lightweight vehicles have high performance.",
-    color = "gray70",
-    fill = "gray90",
-    concavity = 5,
-    # control width text
-    label.minwidth = unit(100, "mm"),
-    # how much distance before show legend
-    label.buffer = unit(2.5, "mm"),
-    label.colour = "gray30",
-    label.family = "ibm"
-  ) +
-  # use circle for points
-  geom_mark_circle(
-    aes(filter =  wt > 3.75, label = NULL, description = name),
-    color = "gray70",
-    fill = "transparent",
-    label.fontsize = 10,
-    label.family = "ibm"
-   )
-
-

+
+Code +
library(ggforce)
+
+p1 +
+  geom_mark_hull(
+    aes(filter =  mpg > 30, label = "Interesting"),
+    description = "Lightweight vehicles have high performance.",
+    color = "gray70",
+    fill = "gray90",
+    concavity = 5,
+    # control width text
+    label.minwidth = unit(100, "mm"),
+    # how much distance before show legend
+    label.buffer = unit(2.5, "mm"),
+    label.colour = "gray30",
+    label.family = "ibm"
+  ) +
+  # use circle for points
+  geom_mark_circle(
+    aes(filter =  wt > 3.75, label = NULL, description = name),
+    color = "gray70",
+    fill = "transparent",
+    label.fontsize = 10,
+    label.family = "ibm"
+   )
+
+
+
+
+

+
+

And other geom_mark_* like hull, circle, ellpse, rect.

-
Code
p2 + 
-  geom_mark_ellipse(
-    aes(fill = species, label = species),
-    alpha = 0.1,
-    color = "transparent", # a nice touch sometimes! (imho)
-    label.colour = "gray30",
-    label.family = "ibm",
-    label.fontsize = 8,
-    # label.fontface = "plain",
-    # this is just for blogpost
-    expand = unit(-5, "mm"),
-    radius = unit(5, "mm")
-    ) +
-  geom_mark_circle(
-    aes(
-      filter = coalesce(bill_length_mm, 0) == max(bill_length_mm, na.rm = TRUE),
-      label = NULL, 
-      description = "A rare penguin!"
-      ),
-    color = "gray70",
-    fill = "transparent",
-    label.fontsize = 8,
-    label.family = "ibm"
-   ) +
-  theme(legend.position = "none") +
-  labs(x = NULL, y = NULL)
-
-
-

+
+Code +
p2 + 
+  geom_mark_ellipse(
+    aes(fill = species, label = species),
+    alpha = 0.1,
+    color = "transparent", # a nice touch sometimes! (imho)
+    label.colour = "gray30",
+    label.family = "ibm",
+    label.fontsize = 8,
+    # label.fontface = "plain",
+    # this is just for blogpost
+    expand = unit(-5, "mm"),
+    radius = unit(5, "mm")
+    ) +
+  geom_mark_circle(
+    aes(
+      filter = coalesce(bill_length_mm, 0) == max(bill_length_mm, na.rm = TRUE),
+      label = NULL, 
+      description = "A rare penguin!"
+      ),
+    color = "gray70",
+    fill = "transparent",
+    label.fontsize = 8,
+    label.family = "ibm"
+   ) +
+  theme(legend.position = "none") +
+  labs(x = NULL, y = NULL)
+
+
+
+
+

Now imagine we used a \(K\)-means algorithm:

-
Code
dcenters <- penguins |>
-  select(species, bill_length_mm, bill_depth_mm) |> 
-  filter(!is.na(bill_length_mm)) |> 
-  filter(!is.na(bill_depth_mm)) |> 
-  group_by(species) |> 
-  summarise(across(everything(), median)) |> 
-  select(-species) |> 
-  mutate(cluster = as.character(row_number()))
-
-dcenters
-
+
+Code +
dcenters <- penguins |>
+  select(species, bill_length_mm, bill_depth_mm) |> 
+  filter(!is.na(bill_length_mm)) |> 
+  filter(!is.na(bill_depth_mm)) |> 
+  group_by(species) |> 
+  summarise(across(everything(), median)) |> 
+  select(-species) |> 
+  mutate(cluster = as.character(row_number()))
+
+dcenters
+
+
# A tibble: 3 × 3
   bill_length_mm bill_depth_mm cluster
            <dbl>         <dbl> <chr>  
@@ -460,217 +550,261 @@ 

My favorite ggplot2 packages with examples

2 49.6 18.4 2 3 47.4 15 3
-
Code
bnd <- penguins |>
-  summarise(
-    min(bill_length_mm, na.rm = TRUE) - 1,
-    max(bill_length_mm, na.rm = TRUE) + 1,
-    min(bill_depth_mm, na.rm = TRUE)  - 1,
-    max(bill_depth_mm, na.rm = TRUE)  + 1
-  ) |> 
-  as.list() |> 
-  unlist() |> 
-  as.vector()
-
-p2 +
-  geom_voronoi_tile(
-    aes(fill = cluster, group = -1),
-    data = dcenters, alpha = 0.2, bound = bnd
-    ) +
-  geom_voronoi_segment(
-    aes(group = -1),
-    data = dcenters, color = "gray90", bound = bnd
-    ) +
-  xlim(bnd[1], bnd[2]) + 
-  ylim(bnd[3], bnd[4]) +
-  scale_fill_viridis_d(direction = -1, option = "C") +
-  # its better put point over all layers
-  geom_point(
-    aes(color = species), 
-    size = 2,
-    shape = 21, 
-    color = "gray90"
-    ) +
-  theme(legend.position = "right")
-
-
-

+
+Code +
bnd <- penguins |>
+  summarise(
+    min(bill_length_mm, na.rm = TRUE) - 1,
+    max(bill_length_mm, na.rm = TRUE) + 1,
+    min(bill_depth_mm, na.rm = TRUE)  - 1,
+    max(bill_depth_mm, na.rm = TRUE)  + 1
+  ) |> 
+  as.list() |> 
+  unlist() |> 
+  as.vector()
+
+p2 +
+  geom_voronoi_tile(
+    aes(fill = cluster, group = -1),
+    data = dcenters, alpha = 0.2, bound = bnd
+    ) +
+  geom_voronoi_segment(
+    aes(group = -1),
+    data = dcenters, color = "gray90", bound = bnd
+    ) +
+  xlim(bnd[1], bnd[2]) + 
+  ylim(bnd[3], bnd[4]) +
+  scale_fill_viridis_d(direction = -1, option = "C") +
+  # its better put point over all layers
+  geom_point(
+    aes(color = species), 
+    size = 2,
+    shape = 21, 
+    color = "gray90"
+    ) +
+  theme(legend.position = "right")
+
+
+
+
+

-

{ggfittext}

+
+
+

{ggfittext}

Source: https://github.com/wilkox/ggfittext.

This package have a lot of features to work with strings in ggplot objects, particulary when you use treemaps.

-

A love the simple and useful function geom_bar_text() + geom_col() combo.

-
-
Code
library(ggfittext)
-
-p3 <- penguins |> 
-  count(species) |> 
-  mutate(n = 100 * n) |> 
-  ggplot(aes(species, n)) +
-  geom_col(width = 0.35, fill = "cyan4") 
-
-p3 +
-  geom_bar_text()
-p3 +
-  geom_bar_text(
-    formatter = scales::comma_format(), 
-    padding.y =  grid::unit(2.5, "mm")
-  )
-p3 + 
-  geom_bar_text(
-    formatter = scales::comma_format(), 
-    place = "bottom",
-    padding.y =  grid::unit(2.5, "mm")
-  )
-
-
-
+

A love the simple and useful function geom_bar_text() + geom_col() combo.

+
+Code +
library(ggfittext)
+
+p3 <- penguins |> 
+  count(species) |> 
+  mutate(n = 100 * n) |> 
+  ggplot(aes(species, n)) +
+  geom_col(width = 0.35, fill = "cyan4") 
+
+p3 +
+  geom_bar_text()
+p3 +
+  geom_bar_text(
+    formatter = scales::comma_format(), 
+    padding.y =  grid::unit(2.5, "mm")
+  )
+p3 + 
+  geom_bar_text(
+    formatter = scales::comma_format(), 
+    place = "bottom",
+    padding.y =  grid::unit(2.5, "mm")
+  )
+
+
+
+

-
+

-
+

-
-

{scales}

+
+
+

{scales}

Source: https://scales.r-lib.org/.

You use this package using a label_* function in the labels argument of scale_* function.

-
Code
library(scales) # comma dollar label_comma
-
-x <- c(123, 456678, 100000)
-
-c(comma(x), dollar(x))
-
+
+Code +
library(scales) # comma dollar label_comma
+
+x <- c(123, 456678, 100000)
+
+c(comma(x), dollar(x))
+
+
[1] "123"      "456,678"  "100,000"  "$123"     "$456,678" "$100,000"
-
Code
# I know the correct alternative is Mpg
-miles_per_gallon <- label_comma(suffix = " mi/gal")
-
-wt_lbl <- label_comma(scale = 1000, suffix = " lbs")
-
-p1 +
-  scale_y_continuous(
-    labels = miles_per_gallon, 
-    name = "fuel consumption"
-    ) +
-  scale_x_continuous(
-    labels = wt_lbl, 
-    name = "weigth"
-    )
-
-

-
-
-

{ggparty}

+
+Code +
# I know the correct alternative is Mpg
+miles_per_gallon <- label_comma(suffix = " mi/gal")
+
+wt_lbl <- label_comma(scale = 1000, suffix = " lbs")
+
+p1 +
+  scale_y_continuous(
+    labels = miles_per_gallon, 
+    name = "fuel consumption"
+    ) +
+  scale_x_continuous(
+    labels = wt_lbl, 
+    name = "weigth"
+    )
+
+
+
+
+

+
+
+
+
+ +
+

{ggparty}

Source: https://github.com/martin-borkovec/ggparty.

-
Code
library(partykit) # ctree
-library(ggparty)
-
-penguinct <- ctree(
-  species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g + sex,
-  data = penguins
-  )
-
-autoplot(penguinct)
-
-

+
+Code +
library(partykit) # ctree
+library(ggparty)
+
+penguinct <- ctree(
+  species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g + sex,
+  data = penguins
+  )
+
+autoplot(penguinct)
+
+
+
+
+

+
+
-
Code
ggparty(penguinct) +
-  geom_edge(color = "gray80") +
-  geom_edge_label(color = "gray50", size = 4) +
-  geom_node_label(
-    aes(label = splitvar),
-    color = "gray30",
-    label.col = NA, # no box
-    size = 4,
-    label.padding = unit(0.5, "lines"),
-    ids = "inner"
-    ) +
-  geom_node_plot(
-    gglist = list(
-      geom_point(
-        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
-        size = 1, alpha = 0.5
-        ),
-      scale_color_viridis_d(end = 0.9),
-      guides(color = guide_legend(override.aes = list(size = 5))), 
-      theme_minimal(),
-      theme(axis.text = element_text(size = 7)),
-      labs(x = NULL, y = NULL)
-    ),
-    scales = "fixed",
-    id = "terminal"
-  ) +
-  geom_node_label(
-    aes(label = sprintf("Node %s (n = %s)", id, nodesize)),
-    ids = "terminal",
-    size = 3,
-    label.col = NA, # no box
-    nudge_y = 0.01
-    )
-
-
-

+
+Code +
ggparty(penguinct) +
+  geom_edge(color = "gray80") +
+  geom_edge_label(color = "gray50", size = 4) +
+  geom_node_label(
+    aes(label = splitvar),
+    color = "gray30",
+    label.col = NA, # no box
+    size = 4,
+    label.padding = unit(0.5, "lines"),
+    ids = "inner"
+    ) +
+  geom_node_plot(
+    gglist = list(
+      geom_point(
+        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
+        size = 1, alpha = 0.5
+        ),
+      scale_color_viridis_d(end = 0.9),
+      guides(color = guide_legend(override.aes = list(size = 5))), 
+      theme_minimal(),
+      theme(axis.text = element_text(size = 7)),
+      labs(x = NULL, y = NULL)
+    ),
+    scales = "fixed",
+    id = "terminal"
+  ) +
+  geom_node_label(
+    aes(label = sprintf("Node %s (n = %s)", id, nodesize)),
+    ids = "terminal",
+    size = 3,
+    label.col = NA, # no box
+    nudge_y = 0.01
+    )
+
+
+
+
+

-

{parttree}

+
+
+

{parttree}

Source: https://github.com/grantmcdermott/parttree.

I use the {parttree} package when the model is simple, or when I want to explain the decision tree algorithm.

-
Code
# remotes::install_github("grantmcdermott/parttree")
-library(parttree)
-
-# 2 independent variables
-penguinct2 <- ctree(
-  species ~ bill_length_mm + bill_depth_mm,
-  data = penguins,
-  control = ctree_control(maxdepth = 3)
-  )
-
-ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
-  geom_parttree(
-    data = penguinct2, 
-    aes(fill = species),
-    alpha = 0.2, 
-    color = "gray60",
-    ) + 
-  geom_point(aes(col = species)) +
-  theme_minimal()
-
-

-
-
-

-{ggparty} + {parttree} -

+
+Code +
# remotes::install_github("grantmcdermott/parttree")
+library(parttree)
+
+# 2 independent variables
+penguinct2 <- ctree(
+  species ~ bill_length_mm + bill_depth_mm,
+  data = penguins,
+  control = ctree_control(maxdepth = 3)
+  )
+
+ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
+  geom_parttree(
+    data = penguinct2, 
+    aes(fill = species),
+    alpha = 0.2, 
+    color = "gray60",
+    ) + 
+  geom_point(aes(col = species)) +
+  theme_minimal()
+
+
+
+
+

+
+
+
+
+ +
+

{ggparty} + {parttree}

-
Code
dpred_node <- penguins |>
-  select(species, bill_length_mm, bill_depth_mm) |>
-  mutate(
-    id = predict(penguinct2, type = "node", newdata = penguins),
-    species_pred = predict(penguinct2, newdata = penguins)
-    ) |>
-  group_by(id) |>
-  summarise(
-    species = unique(species_pred),
-    bill_length_mm = mean(bill_length_mm),
-    bill_depth_mm = mean(bill_depth_mm)
-  )
-
-dpred_node
-
+
+Code +
dpred_node <- penguins |>
+  select(species, bill_length_mm, bill_depth_mm) |>
+  mutate(
+    id = predict(penguinct2, type = "node", newdata = penguins),
+    species_pred = predict(penguinct2, newdata = penguins)
+    ) |>
+  group_by(id) |>
+  summarise(
+    species = unique(species_pred),
+    bill_length_mm = mean(bill_length_mm),
+    bill_depth_mm = mean(bill_depth_mm)
+  )
+
+dpred_node
+
+
# A tibble: 5 × 4
      id species   bill_length_mm bill_depth_mm
   <int> <fct>              <dbl>         <dbl>
@@ -680,133 +814,146 @@ 

My favorite ggplot2 packages with examples

4 8 Adelie 43.0 18.2 5 9 Chinstrap 49.5 18.4
-
Code
dparttree <- parttree(penguinct2)
-dparttree <- dparttree |> 
-  as_tibble() |> 
-  rename(id = node) |> 
-  select(-path)
-
-ggp <- ggparty(penguinct2) 
-
-ggp$data <- ggp$data |> 
-  as_tibble() |> 
-  left_join(
-    dpred_node |> select(id, species),
-    by = join_by(id)
-    )
-
-ggp +
-  geom_edge(color = "gray80") +
-  geom_edge_label(color = "gray50", size = 4) +
-  geom_node_label(
-    aes(label = str_replace_all(splitvar, "_", " ")),
-    color = "gray30",
-    label.col = NA, # no box
-    size = 4,
-    label.padding = unit(0.5, "lines"),
-    ids = "inner"
-    ) +
-  geom_node_plot(
-    gglist = list(
-      geom_point(
-        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
-        size = 1, alpha = 0.5
-        ),
-      geom_parttree(
-        data = penguinct2, 
-        aes(fill = species),
-        alpha = 0.1, 
-        color = "gray60",
-        ),
-      
-      geom_point(
-        data = dpred_node, 
-        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
-        size = 3
-        ),
-      
-      geom_rect(
-        data = dparttree, 
-        aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = species),
-        alpha = 0.5, 
-        color = "gray40",
-        ),
-      
-      scale_fill_manual(values = colors),
-      scale_color_manual(values = colors),
-      # scale_color_viridis_d(end = 0.9),
-      # scale_fill_viridis_d(end = 0.9),
-      guides(color = guide_legend(override.aes = list(size=5))),
-      theme_minimal(),
-      theme(axis.text = element_text(size = 7)),
-      labs(x = NULL, y = NULL)
-    ),
-    scales = "fixed",
-    # id = "all"
-    id = "terminal"
-  ) +
-  geom_node_label(
-    aes(label = str_glue("{species} (#{id},  n = {nodesize})")),
-    ids = "terminal",
-    size = 3,
-    family = "ibm",
-    label.col = NA, # no box
-    nudge_y = 0.01
-    )
-
-
-

+
+Code +
dparttree <- parttree(penguinct2)
+dparttree <- dparttree |> 
+  as_tibble() |> 
+  rename(id = node) |> 
+  select(-path)
+
+ggp <- ggparty(penguinct2) 
+
+ggp$data <- ggp$data |> 
+  as_tibble() |> 
+  left_join(
+    dpred_node |> select(id, species),
+    by = join_by(id)
+    )
+
+ggp +
+  geom_edge(color = "gray80") +
+  geom_edge_label(color = "gray50", size = 4) +
+  geom_node_label(
+    aes(label = str_replace_all(splitvar, "_", " ")),
+    color = "gray30",
+    label.col = NA, # no box
+    size = 4,
+    label.padding = unit(0.5, "lines"),
+    ids = "inner"
+    ) +
+  geom_node_plot(
+    gglist = list(
+      geom_point(
+        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
+        size = 1, alpha = 0.5
+        ),
+      geom_parttree(
+        data = penguinct2, 
+        aes(fill = species),
+        alpha = 0.1, 
+        color = "gray60",
+        ),
+      
+      geom_point(
+        data = dpred_node, 
+        aes(x = bill_length_mm, y = bill_depth_mm, color = species),
+        size = 3
+        ),
+      
+      geom_rect(
+        data = dparttree, 
+        aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = species),
+        alpha = 0.5, 
+        color = "gray40",
+        ),
+      
+      scale_fill_manual(values = colors),
+      scale_color_manual(values = colors),
+      # scale_color_viridis_d(end = 0.9),
+      # scale_fill_viridis_d(end = 0.9),
+      guides(color = guide_legend(override.aes = list(size=5))),
+      theme_minimal(),
+      theme(axis.text = element_text(size = 7)),
+      labs(x = NULL, y = NULL)
+    ),
+    scales = "fixed",
+    # id = "all"
+    id = "terminal"
+  ) +
+  geom_node_label(
+    aes(label = str_glue("{species} (#{id},  n = {nodesize})")),
+    ids = "terminal",
+    size = 3,
+    family = "ibm",
+    label.col = NA, # no box
+    nudge_y = 0.01
+    )
+
+
+
+
+

-

{patchwork}

+
+
+

{patchwork}

Source: https://patchwork.data-imaginist.com/articles/patchwork.html.

-
Code
library(patchwork)
-
-p2 <- p2 +
-  scale_color_manual(values = colors) +
-  theme(legend.position = "none")
-
-p3 <- p3 +
-  geom_col(aes(fill = species), width = 0.5) +
-  scale_fill_manual(values = colors, name = NULL)
-
-pp <- ((p1 / p3) | p2) +
-  plot_layout(
-    widths = c(1, 2),
-    guides = "collect"
-    ) +
-  plot_annotation(
-    title = "Some ggplot2 objects",
-    subtitle = "The plot (a) shows one aspect, while (b) presents additional data.",
-    tag_levels = "a",
-    tag_prefix = "(",
-    tag_suffix = ")"
-    )
-
-pp
-
-
-

+
+Code +
library(patchwork)
+
+p2 <- p2 +
+  scale_color_manual(values = colors) +
+  theme(legend.position = "none")
+
+p3 <- p3 +
+  geom_col(aes(fill = species), width = 0.5) +
+  scale_fill_manual(values = colors, name = NULL)
+
+pp <- ((p1 / p3) | p2) +
+  plot_layout(
+    widths = c(1, 2),
+    guides = "collect"
+    ) +
+  plot_annotation(
+    title = "Some ggplot2 objects",
+    subtitle = "The plot (a) shows one aspect, while (b) presents additional data.",
+    tag_levels = "a",
+    tag_prefix = "(",
+    tag_suffix = ")"
+    )
+
+pp
+
+
+
+
+

-

Reuse

Citation

BibTeX citation:
@online{kunstfuentes2023,
-  author = {Joshua Kunst Fuentes},
+
+ +

Reuse

Citation

BibTeX citation:
@online{kunst fuentes2023,
+  author = {Kunst Fuentes, Joshua},
   title = {My Favorite `Ggplot2` Packages with Examples},
   date = {2023-07-23},
   url = {https://jkunst.com/blog/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples},
   langid = {en}
 }
-
For attribution, please cite this work as:
-Joshua Kunst Fuentes. 2023. “My Favorite `Ggplot2` Packages with +
For attribution, please cite this work as:
+Kunst Fuentes, Joshua. 2023. “My Favorite `Ggplot2` Packages with Examples.” July 23, 2023. https://jkunst.com/blog/posts/2023-07-10-my-favorite-ggplot2-packages-with-examples. -
+ + +
-