apexcharter/vignettes/extra/facets.Rmd

133 lines
3.4 KiB
Plaintext
Raw Normal View History

2020-12-14 17:41:35 +01:00
---
2021-10-21 11:55:32 +02:00
title: "Create grid of charts"
2020-12-14 17:41:35 +01:00
output: rmarkdown::html_vignette
vignette: >
2021-10-21 11:55:32 +02:00
%\VignetteIndexEntry{Create grid of charts}
2020-12-14 17:41:35 +01:00
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
options(rmarkdown.html_vignette.check_title = FALSE)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(apexcharter)
```
Create grid of charts with ApexCharts, currently it's possible to:
- create grids according to one or more variables
- define number of columns and rows
- define type of scale for x-axis and y-axis (fixed or free)
- synchronize charts within the grid
Current limitations are :
- need specific render and output function in Shiny (`apexfacetOutput()` and `renderApexfacet()`)
- x-axis always appear for scatter and line charts
- x-axis labels can differ between charts even with fixed scale depending on the width of the chart and the formatter applied to labels
- when scale on an axis is fixed, the chart with the axis don't have the exact same size than the other since the axis take space in the plotting area
2021-01-08 11:33:13 +01:00
- if legend is needed, it will appear on each charts
2020-12-14 17:41:35 +01:00
## Facet wrap
2021-01-06 10:05:37 +01:00
Create a grid of charts according to a variable of the data with `ax_facet_wrap()` :
2020-12-14 17:41:35 +01:00
2021-01-06 10:05:37 +01:00
```{r facet-wrap}
2021-01-08 14:17:17 +01:00
library(apexcharter)
2020-12-14 17:41:35 +01:00
data("mpg", package = "ggplot2")
2021-01-06 10:05:37 +01:00
2020-12-14 17:41:35 +01:00
apex(mpg, aes(displ, cty), type = "scatter") %>%
2021-01-08 14:17:17 +01:00
ax_xaxis(labels = list(formatter = format_num(".0f"))) %>%
2021-01-08 11:33:13 +01:00
ax_labs(
title = "Facet wrap example",
2021-01-08 14:17:17 +01:00
subtitle = "mpg data from ggplot2",
x = "engine displacement, in litres",
y = "city miles per gallon"
2021-01-08 11:33:13 +01:00
) %>%
2020-12-14 17:41:35 +01:00
ax_facet_wrap(vars(drv), ncol = 2)
```
Synchronized line charts with free y-axis :
2021-01-06 10:05:37 +01:00
```{r facet-wrap-sync}
2021-01-08 14:17:17 +01:00
library(apexcharter)
2021-01-06 10:05:37 +01:00
data("economics_long", package = "ggplot2")
apex(economics_long, aes(date, value), type = "line", synchronize = "sync-it") %>%
ax_yaxis(
decimalsInFloat = 0,
labels = list(
formatter = format_num("~s"),
minWidth = 40
)
) %>%
2020-12-14 17:41:35 +01:00
ax_tooltip(x = list(format = "yyyy")) %>%
2021-01-06 10:05:37 +01:00
ax_facet_wrap(vars(variable), scales = "free_y")
```
Don't forget to set a `minWidth` for y axis labels when synchronizing charts, otherwise unexpected results can occurs.
## Facet grid
Create a matrix of charts defined by row and column faceting variables with `ax_facet_grid()` :
```{r facet-grid}
2021-01-08 14:17:17 +01:00
library(apexcharter)
2021-01-06 10:05:37 +01:00
data("mpg", package = "ggplot2")
apex(mpg, aes(displ, cty), type = "scatter") %>%
2021-01-08 14:17:17 +01:00
ax_xaxis(labels = list(formatter = format_num(".0f"))) %>%
2021-01-08 11:33:13 +01:00
ax_labs(
title = "Facet grid example",
2021-01-08 14:17:17 +01:00
subtitle = "mpg data from ggplot2",
x = "engine displacement, in litres",
y = "city miles per gallon"
2021-01-08 11:33:13 +01:00
) %>%
2021-01-06 10:05:37 +01:00
ax_facet_grid(rows = vars(drv), cols = vars(year))
2020-12-14 17:41:35 +01:00
```
2021-01-06 10:05:37 +01:00
2021-01-04 12:02:48 +01:00
## Grid
You can construct a grid of (unrelated) charts with `apex_grid()`, construct your charts independently then assemble them in the grid:
2021-01-06 10:05:37 +01:00
```{r apex-grid}
2021-01-08 14:17:17 +01:00
library(apexcharter)
data("mpg", package = "ggplot2")
# Construct 3 charts
2021-01-04 12:02:48 +01:00
a1 <- apex(mpg, aes(manufacturer), type = "bar")
a2 <- apex(mpg, aes(trans), type = "column")
a3 <- apex(mpg, aes(drv), type = "pie")
2021-01-08 14:17:17 +01:00
# Assemble them in a grid
2021-01-04 12:02:48 +01:00
apex_grid(
a1, a2, a3,
grid_area = c("1 / 1 / 3 / 2", "1 / 2 / 2 / 4", "2 / 2 / 3 / 4"),
ncol = 3,
nrow = 2,
height = "600px"
)
```
`grid_area` argument allow to specify space occupied by each chart, you can generate interactively your grid template [here](https://cssgrid-generator.netlify.app/).
2020-12-14 17:41:35 +01:00