apexcharter/vignettes/apexcharter.Rmd

228 lines
4.7 KiB
Plaintext
Raw Normal View History

2019-02-18 20:29:51 +01:00
---
title: "Starting with ApexCharts"
author: "Victor Perrier"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Starting with ApexCharts}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The objective of this vignette is to show how to quickly build data visualizations with the ApexCharts JavaScript library, as well as to give an overview of the different graphics available.
Data used are from `ggplot2` package, data manipulation will be done with the `dplyr` package.
2019-05-15 11:53:53 +02:00
```{r message=FALSE, warning=FALSE}
2019-02-18 20:29:51 +01:00
library(ggplot2)
2019-11-26 10:34:01 +01:00
library(scales)
2019-02-18 20:29:51 +01:00
library(dplyr)
library(apexcharter)
```
## Bar charts
Simple bar charts can be created with:
2019-04-06 12:10:05 +02:00
```{r column}
2019-02-18 20:29:51 +01:00
data("mpg")
n_manufac <- count(mpg, manufacturer)
apex(data = n_manufac, type = "column", mapping = aes(x = manufacturer, y = n))
```
Flipping coordinates can be done by using `type = "bar"`:
2019-04-06 12:10:05 +02:00
```{r bar}
2019-02-18 20:29:51 +01:00
apex(data = n_manufac, type = "bar", mapping = aes(x = manufacturer, y = n))
```
To create a dodge bar charts, use aesthetic `fill` :
2019-04-06 12:10:05 +02:00
```{r dodge-bar}
2019-02-18 20:29:51 +01:00
n_manufac_year <- count(mpg, manufacturer, year)
apex(data = n_manufac_year, type = "column", mapping = aes(x = manufacturer, y = n, fill = year))
```
For stacked bar charts, specify option `stacked` in `ax_chart` :
2019-04-06 12:10:05 +02:00
```{r stacked-bar}
2019-02-18 20:29:51 +01:00
apex(data = n_manufac_year, type = "column", mapping = aes(x = manufacturer, y = n, fill = year)) %>%
ax_chart(stacked = TRUE)
```
## Line charts
Simple line charts can be created with (works with `character`, `Date` or `POSIXct`):
2019-04-06 12:10:05 +02:00
```{r line}
2019-02-18 20:29:51 +01:00
data("economics")
economics <- tail(economics, 100)
2019-02-18 20:29:51 +01:00
apex(data = economics, type = "line", mapping = aes(x = date, y = uempmed))
```
To represent several lines, use a `data.frame` in long format and the `group` aesthetic:
2019-04-06 12:10:05 +02:00
```{r lines}
data("economics_long")
2019-02-18 20:29:51 +01:00
economics_long <- economics_long %>%
group_by(variable) %>%
slice((n()-100):n())
2019-02-18 20:29:51 +01:00
2019-05-15 12:02:36 +02:00
apex(data = economics_long, type = "line", mapping = aes(x = date, y = value01, group = variable)) %>%
ax_yaxis(decimalsInFloat = 2) # number of decimals to keep
2019-02-18 20:29:51 +01:00
```
Create area charts with `type = "area"`:
2019-04-06 12:10:05 +02:00
```{r area}
2019-02-18 20:29:51 +01:00
apex(data = economics_long, type = "area", mapping = aes(x = date, y = value01, fill = variable)) %>%
2019-05-15 12:02:36 +02:00
ax_yaxis(decimalsInFloat = 2) %>% # number of decimals to keep
2019-05-29 13:04:35 +02:00
ax_chart(stacked = TRUE) %>%
2019-06-04 10:25:17 +02:00
ax_yaxis(max = 4, tickAmount = 4)
2019-02-18 20:29:51 +01:00
```
## Scatter charts
Simple bar charts can be created with:
2019-04-06 12:10:05 +02:00
```{r scatter}
2019-02-18 20:29:51 +01:00
apex(data = mtcars, type = "scatter", mapping = aes(x = wt, y = mpg))
```
Color points according to a third variable:
2019-04-06 12:10:05 +02:00
```{r scatter-fill}
2020-01-27 18:21:05 +01:00
apex(data = mtcars, type = "scatter", mapping = aes(x = wt, y = mpg, fill = cyl))
2019-02-18 20:29:51 +01:00
```
And change point size using `z` aesthetics:
2019-04-06 12:10:05 +02:00
```{r bubbles}
2019-02-18 20:29:51 +01:00
apex(data = mtcars, type = "scatter", mapping = aes(x = wt, y = mpg, z = scales::rescale(qsec)))
```
## Pie charts
Simple pie charts can be created with:
2019-04-06 12:10:05 +02:00
```{r pie}
2019-02-18 20:29:51 +01:00
poll <- data.frame(
answer = c("Yes", "No"),
n = c(254, 238)
)
apex(data = poll, type = "pie", mapping = aes(x = answer, y = n))
```
## Radial charts
Simple radial charts can be created with (here we pass values directly in `aes`, but you can use a `data.frame`) :
2019-04-06 12:10:05 +02:00
```{r radial}
2019-02-18 20:29:51 +01:00
apex(data = NULL, type = "radialBar", mapping = aes(x = "My value", y = 65))
```
Multi radial chart (more than one value):
2019-04-06 12:10:05 +02:00
```{r radial-mult}
2019-02-18 20:29:51 +01:00
fruits <- data.frame(
name = c('Apples', 'Oranges', 'Bananas', 'Berries'),
value = c(44, 55, 67, 83)
)
apex(data = fruits, type = "radialBar", mapping = aes(x = name, y = value))
```
## Radar charts
Simple radar charts can be created with:
2019-04-06 12:10:05 +02:00
```{r radar}
2019-02-18 20:29:51 +01:00
mtcars$model <- rownames(mtcars)
apex(data = head(mtcars), type = "radar", mapping = aes(x = model, y = qsec))
```
With a grouping variable:
2019-04-06 12:10:05 +02:00
```{r radar-mult}
2019-02-18 20:29:51 +01:00
# extremely complicated reshaping
new_mtcars <- reshape(
data = head(mtcars),
idvar = "model",
varying = list(c("drat", "wt")),
times = c("drat", "wt"),
direction = "long",
v.names = "value",
drop = c("mpg", "cyl", "hp", "dist", "qsec", "vs", "am", "gear", "carb")
)
apex(data = new_mtcars, type = "radar", mapping = aes(x = model, y = value, group = time))
```
## Heatmap
Create heatmap with :
2019-04-06 12:10:05 +02:00
```{r heatmap}
2019-02-18 20:29:51 +01:00
txhousing2 <- txhousing %>%
filter(city %in% head(unique(city)), year %in% c(2000, 2001)) %>%
rename(val_med = median)
2019-07-19 14:11:41 +02:00
apex(
data = txhousing2,
type = "heatmap",
mapping = aes(x = date, y = city, fill = scales::rescale(val_med))
) %>%
2019-02-18 20:29:51 +01:00
ax_dataLabels(enabled = FALSE) %>%
ax_colors("#008FFB")
```
2020-06-13 13:04:23 +02:00
## Candlestick
Create a candlestick chart with:
```{r}
apex(
candles,
aes(x = datetime, open = open, close = close, low = low, high = high),
type = "candlestick"
)
```
2019-02-18 20:29:51 +01:00