bug scales + examples

This commit is contained in:
pvictor 2021-01-05 15:08:13 +01:00
parent 730d6651a0
commit 175271d01a
7 changed files with 71 additions and 24 deletions

View File

@ -70,18 +70,23 @@ set_scale <- function(ax, values, scales = c("fixed", "free", "free_y", "free_x"
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% fmt(range_vals[1])
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% fmt(range_vals[2])
} else if (scales == "free") {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% character(0)
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% character(0)
} else {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% fmt(range_vals[1])
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% fmt(range_vals[2])
if (scales == "free_x" & axis == "x") {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% character(0)
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% character(0)
ax$x$ax_opts[[waxis]]$min <- NULL
ax$x$ax_opts[[waxis]]$max <- NULL
} else if (scales == "free_x") {
if (axis == "y") {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% fmt(range_vals[1])
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% fmt(range_vals[2])
} else {
ax$x$ax_opts[[waxis]]$min <- NULL
ax$x$ax_opts[[waxis]]$max <- NULL
}
if (scales == "free_y" & axis == "y") {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% character(0)
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% character(0)
} else if (scales == "free_y") {
if (axis == "x") {
ax$x$ax_opts[[waxis]]$min <- ax$x$ax_opts[[waxis]]$min %||% fmt(range_vals[1])
ax$x$ax_opts[[waxis]]$max <- ax$x$ax_opts[[waxis]]$max %||% fmt(range_vals[2])
} else {
ax$x$ax_opts[[waxis]]$min <- NULL
ax$x$ax_opts[[waxis]]$max <- NULL
}
}
@ -220,6 +225,8 @@ ax_facet_wrap <- function(ax,
#' @export
#'
#' @rdname apex-facets
#'
#' @example examples/facet_grid.R
ax_facet_grid <- function(ax,
rows = NULL,
cols = NULL,
@ -275,8 +282,8 @@ build_facet_tag <- function(x) {
content <- tagList(
lapply(
X = facets$label_col,
FUN = function(x) {
tags$div(x$x$facet$labeller(x), class = "apexcharter-facet-col-label")
FUN = function(label_col) {
tags$div(x$x$facet$labeller(label_col), class = "apexcharter-facet-col-label")
}
),
if (!is.null(facets$nrow)) tags$div(),

View File

@ -1,5 +1,5 @@
get_grid_dims <- function(content, nrow, ncol) {
get_grid_dims <- function(content, nrow = NULL, ncol = NULL) {
n <- length(content)
if (is.null(nrow) & !is.null(ncol))
nrow <- ceiling(n / ncol)

View File

@ -19,9 +19,9 @@ ui <- fluidPage(
server <- function(input, output, session) {
output$myfacet <- renderApexfacet({
apex(refugees, aes(date, n), type = "line") %>%
apex(refugees, aes(date, n), type = "column") %>%
ax_yaxis(tickAmount = 5) %>%
ax_facet_wrap(vars(continent_origin))
ax_facet_wrap(vars(continent_origin), scales = "free")
})
}

View File

@ -1,3 +1,6 @@
### Grid --------
library(apexcharter)
# Scatter ----
@ -17,6 +20,7 @@ apex(mpg, aes(displ, cty), type = "scatter") %>%
ax_facet_grid(rows = vars(drv), cols = vars(year))
apex(mpg, aes(displ, cty), type = "scatter") %>%
ax_chart(toolbar = list(show = FALSE)) %>%
ax_facet_grid(vars(drv), vars(cyl))

View File

@ -1,3 +1,4 @@
### Wrap --------
library(apexcharter)
# Scatter ----
@ -50,8 +51,6 @@ apex(mpg, aes(displ, cty), type = "scatter") %>%
# Lines ----
data("unhcr_ts")
@ -75,7 +74,6 @@ apex(refugees, aes(date, n), type = "line", synchronize = "my-id") %>%
# Bars ----
data("unhcr_ts")

View File

@ -48,6 +48,7 @@ An \code{apexcharts} \code{htmlwidget} object.
Facet wrap for ApexCharts
}
\examples{
### Wrap --------
library(apexcharter)
# Scatter ----
@ -100,8 +101,6 @@ apex(mpg, aes(displ, cty), type = "scatter") \%>\%
# Lines ----
data("unhcr_ts")
@ -125,7 +124,6 @@ apex(refugees, aes(date, n), type = "line", synchronize = "my-id") \%>\%
# Bars ----
data("unhcr_ts")
@ -142,4 +140,44 @@ apex(refugees, aes(continent_origin, n), type = "column") \%>\%
ax_facet_wrap(vars(population_type), ncol = 2)
### Grid --------
library(apexcharter)
# Scatter ----
data("mpg", package = "ggplot2")
# Only rows
apex(mpg, aes(displ, cty), type = "scatter") \%>\%
ax_facet_grid(rows = vars(drv), chart_height = "200px")
# Only cols
apex(mpg, aes(displ, cty), type = "scatter") \%>\%
ax_facet_grid(cols = vars(year))
# Rows and Cols
apex(mpg, aes(displ, cty), type = "scatter") \%>\%
ax_facet_grid(rows = vars(drv), cols = vars(year))
apex(mpg, aes(displ, cty), type = "scatter") \%>\%
ax_chart(toolbar = list(show = FALSE)) \%>\%
ax_facet_grid(vars(drv), vars(cyl))
# Labels
apex(mpg, aes(displ, cty), type = "scatter") \%>\%
ax_facet_grid(
vars(drv),
labeller = function(x) {
switch(
x,
"f" = "front-wheel drive",
"r" = "rear wheel drive",
"4" = "4wd"
)
}
)
}

View File

@ -49,9 +49,9 @@ ui <- fluidPage(
server <- function(input, output, session) {
output$myfacet <- renderApexfacet({
apex(refugees, aes(date, n), type = "line") \%>\%
apex(refugees, aes(date, n), type = "column") \%>\%
ax_yaxis(tickAmount = 5) \%>\%
ax_facet_wrap(vars(continent_origin))
ax_facet_wrap(vars(continent_origin), scales = "free")
})
}