library(apexcharter) library(dplyr) # Bar --------------------------------------------------------------------- data("mpg", package = "ggplot2") count(mpg, class) %>% mutate(pct=n/sum(n)) %>% apex(mapping = aes(class, pct), type = "column") %>% ax_colors("#617a89") %>% ax_labs( title = "Seminal ggplot2 column chart example with percents", subtitle = "Example taken from {hrbrthemes} readme" ) %>% ax_yaxis( title = list(text = "Weight (tons)"), labels = list( formatter = JS("function(value) {return Math.round(value * 100) + '%';}") ), tickAmount = 6 ) %>% ax_xaxis( title = list(text = "Fuel efficiency (mpg)") ) %>% ax_title( style = list(fontSize = "22px") ) %>% ax_subtitle( style = list(fontSize = "16px", color = "#BDBDBD") ) # Line -------------------------------------------------------------------- data("economics_long", package = "ggplot2") economics_long %>% filter(variable %in% c("psavert", "uempmed")) %>% mutate(variable = case_when( variable == "psavert" ~ "Personal savings rate", variable == "uempmed" ~ "Median duration of unemployment (in weeks)" )) %>% apex(aes(date, value, group = variable), type = "line") %>% ax_legend(position = "bottom") %>% ax_stroke(width = 1) %>% ax_yaxis( min = 0, max = 30, tickAmount = 6, labels = list( formatter = htmlwidgets::JS("function(val) {return val.toFixed(0);}") ), title = list(text = "Personal savings rate") ) %>% ax_xaxis(type = "datetime", labels = list(format = "d MMM yy")) %>% ax_tooltip(x = list(format = "dd MMM yyyy")) %>% ax_labs( title = "US economic time series", subtitle = "Data from {ggplot2}" ) %>% ax_title( style = list(fontSize = "22px") ) %>% ax_subtitle( style = list(fontSize = "16px", color = "#BDBDBD") ) # Scatter ----------------------------------------------------------------- data("gapminder", package = "gapminder") gapminder %>% filter(year == 2007) %>% mutate( gdpPercap = log(gdpPercap), pop = sqrt(pop/pi)/1500 ) %>% apex(mapping = aes(gdpPercap, lifeExp, z = pop, group = continent, label = country), type = "scatter") %>% ax_chart(zoom = list( enabled = TRUE, type = "xy" )) %>% ax_yaxis(decimalsInFloat = 0, axisBorder = list(show = TRUE), axisTicks = list(show = TRUE)) %>% ax_xaxis(tickAmount = 8, tooltip = list(enabled = FALSE)) %>% ax_grid(xaxis = list(lines = list(show = TRUE))) %>% ax_legend(position = "right") %>% ax_markers(hover = list(sizeOffset = 0, size = 25)) %>% ax_tooltip(custom = JS(paste( "function({ series, seriesIndex, dataPointIndex, w }) {", "console.log(w); return (", "'