labs adjustments

This commit is contained in:
pvictor 2020-04-29 11:14:54 +02:00
parent 6e91a515de
commit 6ebd778537
2 changed files with 42 additions and 10 deletions

View File

@ -25,16 +25,30 @@
#' ) #' )
ax_labs <- function(ax, title = NULL, subtitle = NULL, x = NULL, y = NULL) { ax_labs <- function(ax, title = NULL, subtitle = NULL, x = NULL, y = NULL) {
if (!is.null(title)) { if (!is.null(title)) {
ax <- ax_title(ax = ax, text = title) ax <- ax_title(
ax = ax,
text = title,
style = list(fontWeight = 700, fontSize = "16px")
)
} }
if (!is.null(subtitle)) { if (!is.null(subtitle)) {
ax <- ax_subtitle(ax = ax, text = subtitle) ax <- ax_subtitle(
ax = ax,
text = subtitle,
style = list(fontWeight = 400, fontSize = "14px")
)
} }
if (!is.null(x)) { if (!is.null(x)) {
ax <- ax_xaxis(ax = ax, title = list(text = x)) ax <- ax_xaxis(
ax = ax,
title = list(text = x, style = list(fontWeight = 600, fontSize = "14px"))
)
} }
if (!is.null(y)) { if (!is.null(y)) {
ax <- ax_yaxis(ax = ax, title = list(text = y)) ax <- ax_yaxis(
ax = ax,
title = list(text = y, style = list(fontWeight = 600, fontSize = "14px"))
)
} }
ax ax
} }

View File

@ -22,11 +22,29 @@ library(apexcharter)
library(dplyr) library(dplyr)
data("diamonds", package = "ggplot2") data("diamonds", package = "ggplot2")
n_cut <- dplyr::count(diamonds, cut) n_cut <- count(diamonds, cut)
``` ```
## Chart title ## Labs
You can set title, subtitle and axis' titles at once with `ax_labs()`:
```{r}
apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
ax_labs(
title = "Cut distribution",
subtitle = "Data from ggplot2",
x = "Cut",
y = "Count"
)
```
If you more control (font size, alignment, ...), you can use `ax_title()`, `ax_subtitle()`, `ax_xaxis()` and `ax_yaxis()`, as described below.
## Title
```{r} ```{r}
apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>% apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
@ -40,7 +58,7 @@ apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
ax_title( ax_title(
text = "Cut distribution", text = "Cut distribution",
align = "center", align = "center",
style = list(fontSize = "22px") style = list(fontSize = "22px", fontWeight = 700)
) )
``` ```
@ -48,7 +66,7 @@ apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
Full list of parameters is available here : https://apexcharts.com/docs/options/title/ Full list of parameters is available here : https://apexcharts.com/docs/options/title/
## Chart subtitle ## Subtitle
```{r} ```{r}
apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>% apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
@ -63,12 +81,12 @@ apex(data = n_cut, type = "column", mapping = aes(x = cut, y = n)) %>%
ax_title( ax_title(
text = "Cut distribution", text = "Cut distribution",
align = "center", align = "center",
style = list(fontSize = "22px") style = list(fontSize = "22px", fontWeight = 700)
) %>% ) %>%
ax_subtitle( ax_subtitle(
text = "Data from ggplot2", text = "Data from ggplot2",
align = "center", align = "center",
style = list(fontSize = "16px", color = "#BDBDBD") style = list(fontSize = "16px", fontWeight = 400, color = "#BDBDBD")
) )
``` ```