knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(htmltools)
library(mobileCharts)

Basics

It looks an aweful lot like ggplot2. Initialise the chart with mobile then pipe (%>%) geometries and other options, evey function starts with mobile.

mobile(cars, aes(speed, dist)) %>% 
  mobile_point()

Tooltips

Since this package generates charts for the mobile tooltips work on click not on hover.

mobile(cars, aes(speed, dist)) %>% 
  mobile_point() %>% 
  mobile_tooltip(snap = TRUE)

Pie

mobileCharts is still in early development and some things may look odd, i.e.: how one builds a pie chart. A pie chart is, to mobileCharts, a stacked bar on a polar axis.

df <- data.frame(
 label = c("banana", "apple", "cake"),
 value = c(50, 30, 20),
 x = "1" # phoney variable
)

mobile(df, aes(x, value, color = label, adjust = stack)) %>% 
 mobile_bar() %>% 
 mobile_coord("polar", transposed = TRUE) %>% 
 mobile_hide_axis()

Flip Coordinates

You can also use mobile_coord to flip the coordinates.

df <- data.frame(x = LETTERS[1:3], y = c(10, 20, 15))
mobile(df, aes(x, y)) %>% 
  mobile_bar() %>% 
  mobile_coord("rect", transposed = TRUE)

Legend

You can manipulate the legend with mobile_legend.

mobile(iris, aes(Petal.Length, Petal.Width, color = Species)) %>% 
  mobile_point() %>% 
  mobile_legend(position = "right")

Scale

You can adjust the scaling with mobile_scale.

mobile(cars, aes(speed, dist, size = dist)) %>% 
  mobile_point() %>% 
  mobile_scale_y(max = 150, tickCount = 50)

Heatmap

You can use mobile_polygon to create a heatmap.

df <- expand.grid(x = LETTERS, y = LETTERS)
df$z <- runif(nrow(df))
mobile(df, aes(x, y, color = z)) %>% 
  mobile_polygon()

Combine

You are not limited to a single geom of course.

mobile(mtcars, aes(qsec, wt)) %>% 
  mobile_area() %>% 
  mobile_line() %>% 
  mobile_point(aes(y = mpg, color = "red"))

Interactions

The functions mobile_interaction and mobile_scroll will let you customise interactions with the charts. The latter function simply adds a scrollbar.

df <- data.frame(
  x = 1:20,
  y = runif(20, 1, 10)
)

# pan limits
lmt <- list(x = list(min = -20, max = 20))

mobile(df, aes(x, y)) %>% 
  mobile_bar() %>% 
  mobile_interaction("bar-select") %>% # highlight on select
  mobile_interaction("pan", limitRange = lmt) %>% 
  mobile_scroll(mode = "x", xStyle = list(offsetY = -5))

Themes

You can define your own theme with mobile_theme.

mob <- mobile(iris, aes(Petal.Length, Petal.Width, color = Species)) %>% 
  mobile_point() 

# get plasma colors
colors <- viridis::plasma(4)

mob %>% 
  mobile_theme(colors)

# ggplot2 emulation
mob %>% 
  mobile_ggplot_theme(n = 4)

mob %>% 
  mobile_theme(colors = c("#ffffff"), background_color="#000000")


RinteRface/mobileCharts documentation built on Feb. 14, 2020, 2:31 a.m.