#devtools::install_github("jbkunst/highcharter")
#install.packages("highcharter")
library(highcharter)
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use
Attaching package: ‘highcharter’
The following object is masked _by_ ‘.GlobalEnv’:
stars
data(citytemp)
hc <- highchart() %>%
hc_xAxis(categories = citytemp$month) %>%
hc_add_series(name = "Tokyo", data = citytemp$tokyo) %>%
hc_add_series(name = "London", data = citytemp$london) %>%
hc_add_series(name = "Other city",
data = (citytemp$tokyo + citytemp$london)/2)
hc
library(magrittr)
library(highcharter)
highchart() %>%
hc_title(text = "Scatter chart with size and color") %>%
hc_add_series_scatter(mtcars$wt, mtcars$mpg,
mtcars$drat, mtcars$hp)
'hc_add_series_scatter' is deprecated.
Use 'hc_add_series' instead.
See help("Deprecated")
hc <- hc %>%
hc_chart(type = "column",
options3d = list(enabled = TRUE, beta = 15, alpha = 15))
hc
hc %>%
hc_chart(borderColor = '#EBBA95',
borderRadius = 10,
borderWidth = 2,
backgroundColor = list(
linearGradient = c(0, 0, 500, 500),
stops = list(
list(0, 'rgb(255, 255, 255)'),
list(1, 'rgb(200, 200, 255)')
)))
hc <- highchart() %>%
hc_xAxis(categories = citytemp$month) %>%
hc_add_series(name = "Tokyo", data = citytemp$tokyo) %>%
hc_add_series(name = "New York", data = citytemp$new_york)
hc
hc %>%
hc_add_series(name = "London", data = citytemp$london, type = "area") %>%
hc_rm_series(name = "New York")
hc %>%
hc_title(text = "This is a title with <i>margin</i> and <b>Strong or bold text</b>",
margin = 20, align = "left",
style = list(color = "#90ed7d", useHTML = TRUE)) %>%
hc_subtitle(text = "And this is a subtitle with more information",
align = "left",
style = list(color = "#2b908f", fontWeight = "bold")) %>%
hc_credits(enabled = TRUE, # add credits
text = "www.lonk.tomy.site",
href = "http://jkunst.com") %>%
hc_legend(align = "left", verticalAlign = "top",
layout = "vertical", x = 0, y = 100) %>%
hc_tooltip(crosshairs = TRUE, backgroundColor = "#FCFFC5",
shared = TRUE, borderWidth = 5) %>%
hc_exporting(enabled = TRUE) # enable exporting option
data(diamonds, economics_long, mpg, package = "ggplot2")
library(dplyr)
package ‘dplyr’ was built under R version 3.4.1
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))
mpgman2 <- mpg %>%
count(class, year) %>%
glimpse()
Observations: 14
Variables: 3
$ class <chr> "2seater", "2seater", "compact", "compact", "midsize",...
$ year <int> 1999, 2008, 1999, 2008, 1999, 2008, 1999, 2008, 1999, ...
$ n <int> 2, 3, 25, 22, 20, 21, 6, 5, 16, 17, 19, 16, 29, 33
## Observations: 14
## Variables: 3
## $ class <chr> "2seater", "2seater", "compact", "compact", "midsize", "...
## $ year <int> 1999, 2008, 1999, 2008, 1999, 2008, 1999, 2008, 1999, 20...
## $ n <int> 2, 3, 25, 22, 20, 21, 6, 5, 16, 17, 19, 16, 29, 33
hchart(mpgman2, "column", hcaes(x = class, y = n, group = year))
mpgman3 <- mpg %>%
group_by(manufacturer) %>%
summarise(n = n(), unique = length(unique(model))) %>%
arrange(-n, -unique) %>%
glimpse()
Observations: 15
Variables: 3
$ manufacturer <chr> "dodge", "toyota", "volkswagen", "ford", "chevr...
$ n <int> 37, 34, 27, 25, 19, 18, 14, 14, 13, 9, 8, 5, 4,...
$ unique <int> 4, 6, 4, 4, 4, 3, 2, 2, 3, 1, 1, 1, 1, 1, 1
## Observations: 15
## Variables: 3
## $ manufacturer <chr> "dodge", "toyota", "volkswagen", "ford", "chevrol...
## $ n <int> 37, 34, 27, 25, 19, 18, 14, 14, 13, 9, 8, 5, 4, 4, 3
## $ unique <int> 4, 6, 4, 4, 4, 3, 2, 2, 3, 1, 1, 1, 1, 1, 1
hchart(mpgman3, "treemap", hcaes(x = manufacturer, value = n, color = unique))
economics_long2 <- economics_long %>%
filter(variable %in% c("pop", "uempmed", "unemploy")) %>%
print()
## Source: local data frame [1,722 x 4]
## Groups: variable [3]
##
## date variable value value01
## <date> <fctr> <dbl> <dbl>
## 1 1967-07-01 pop 198712 0.000000000
## 2 1967-08-01 pop 198911 0.001628811
## 3 1967-09-01 pop 199113 0.003282177
## 4 1967-10-01 pop 199311 0.004902803
## 5 1967-11-01 pop 199498 0.006433395
## 6 1967-12-01 pop 199657 0.007734807
## 7 1968-01-01 pop 199808 0.008970739
## 8 1968-02-01 pop 199920 0.009887457
## 9 1968-03-01 pop 200056 0.011000614
## 10 1968-04-01 pop 200208 0.012244731
## # ... with 1,712 more rows
hchart(economics_long2, "line", hcaes(x = date, y = value01, group = variable))
hchart(diamonds$price)
hchart(density(diamonds$price), type = "area", color = "#B71C1C", name = "Price")
hchart(diamonds$cut, type = "column")
hchart(LakeHuron)
x <- stl(log(AirPassengers), "per")
hchart(x)
library("forecast")
x <- forecast(ets(USAccDeaths), h = 48, level = 95)
hchart(x)
library("igraph")
package ‘igraph’ was built under R version 3.4.1
Attaching package: ‘igraph’
The following objects are masked from ‘package:dplyr’:
as_data_frame, groups, union
The following objects are masked from ‘package:stats’:
decompose, spectrum
The following object is masked from ‘package:base’:
union
N <- 40
net <- sample_gnp(N, p = 2/N)
wc <- cluster_walktrap(net)
V(net)$label <- seq(N)
V(net)$name <- paste("I'm #", seq(N))
V(net)$page_rank <- round(page.rank(net)$vector, 2)
V(net)$betweenness <- round(betweenness(net), 2)
V(net)$degree <- degree(net)
V(net)$size <- V(net)$degree
V(net)$comm <- membership(wc)
V(net)$color <- colorize(membership(wc))
hchart(net, layout = layout_with_fr)
library(quantmod)
package ‘quantmod’ was built under R version 3.4.1Loading required package: xts
package ‘xts’ was built under R version 3.4.1Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Attaching package: ‘xts’
The following objects are masked from ‘package:dplyr’:
first, last
Loading required package: TTR
package ‘TTR’ was built under R version 3.4.1Version 0.4-0 included new data defaults. See ?getSymbols.
Learn from a quantmod author: https://www.datacamp.com/courses/importing-and-managing-financial-data-in-r
x <- getSymbols("USD/JPY", src = "oanda", auto.assign = FALSE)
‘getSymbols’ currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
‘loadSymbols’ to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be checked for
alternate defaults.
This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
hchart(x)
The shares of what used to be known as Yahoo under the ticker symbol YHOO started trading Monday as Altaba (think alternative Alibaba) and trade under the symbol AABA.
x <- getSymbols("AABA", auto.assign = FALSE)
WARNING: There have been significant changes to Yahoo Finance data.
Please see the Warning section of ‘?getSymbols.yahoo’ for details.
This message is shown once per session and may be disabled by setting
options("getSymbols.yahoo.warning"=FALSE).
hchart(x)
x <- acf(diff(AirPassengers), plot = FALSE)
hchart(x)
x <- cbind(mdeaths, fdeaths)
hchart(x)
library(survival)
Attaching package: ‘survival’
The following object is masked _by_ ‘.GlobalEnv’:
lung
The following object is masked from ‘package:deepboost’:
heart
data(lung)
lung <- mutate(lung, sex = ifelse(sex == 1, "Male", "Female"))
fit <- survfit(Surv(time, status) ~ sex, data = lung)
hchart(fit, ranges = TRUE)
hchart(princomp(USArrests, cor = TRUE))
data(volcano)
hchart(volcano) %>%
# changing default color
hc_colorAxis(stops = color_stops(colors = viridis::inferno(10)))
mtcars2 <- mtcars[1:20, ]
x <- dist(mtcars2)
hchart(x)
hchart(cor(mtcars))
A better version (in a syntax way) than http://jkunst.com/r/presenting-highcharter/.
#install.packages("rwars")
library(tidyverse)
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
package ‘tibble’ was built under R version 3.4.1package ‘tidyr’ was built under R version 3.4.1Conflicts with tidy packages --------------------------------------------
as_data_frame(): dplyr, tibble, igraph
compose(): purrr, igraph
crossing(): tidyr, igraph
filter(): dplyr, stats
first(): dplyr, xts
groups(): dplyr, igraph
lag(): dplyr, stats
last(): dplyr, xts
simplify(): purrr, igraph
library(rwars)
swmovies <- get_all_films()
swdata <- map_df(swmovies$results, function(x){
data_frame(
movie = x$title,
species = length(x$species),
planets = length(x$planets),
characters = length(x$characters),
vehicles = length(x$vehicles),
release = x$release_date
)
})
swdata <- gather(swdata, key, number, -movie, -release) %>%
arrange(release)
swdata
## # A tibble: 28 × 4
## movie release key number
## <chr> <chr> <chr> <int>
## 1 A New Hope 1977-05-25 species 5
## 2 A New Hope 1977-05-25 planets 3
## 3 A New Hope 1977-05-25 characters 18
## 4 A New Hope 1977-05-25 vehicles 4
## 5 The Empire Strikes Back 1980-05-17 species 5
## 6 The Empire Strikes Back 1980-05-17 planets 4
## 7 The Empire Strikes Back 1980-05-17 characters 16
## 8 The Empire Strikes Back 1980-05-17 vehicles 6
## 9 Return of the Jedi 1983-05-25 species 9
## 10 Return of the Jedi 1983-05-25 planets 5
## # ... with 18 more rows
hchart(swdata, "line", hcaes(x = movie, y = number, group = key),
color = c("#e5b13a", "#4bd5ee", "#4AA942", "#FAFAFA")) %>%
hc_title(
text = "Diversity in <span style=\"color:#e5b13a\"> STAR WARS</span> movies",
useHTML = TRUE) %>%
hc_tooltip(table = TRUE, sort = TRUE) %>%
hc_credits(
enabled = TRUE,
text = "Source: SWAPI via rwars package",
href = "https://swapi.co/") %>%
hc_add_theme(
hc_theme_flatdark(
chart = list(
backgroundColor = "transparent",
divBackgroundImage = "http://www.wired.com/images_blogs/underwire/2013/02/xwing-bg.gif"
)
)
)
data(stars)
colors <- c("#FB1108","#FD150B","#FA7806","#FBE426","#FCFB8F",
"#F3F5E7", "#C7E4EA","#ABD6E6","#9AD2E1")
stars$color <- colorize(log(stars$temp), colors)
x <- c("Luminosity", "Temperature", "Distance")
y <- sprintf("{point.%s:.2f}", c("lum", "temp", "distance"))
tltip <- tooltip_table(x, y)
hchart(stars, "scatter", hcaes(temp, lum, size = radiussun, color = color)) %>%
hc_chart(backgroundColor = "black") %>%
hc_xAxis(type = "logarithmic", reversed = TRUE) %>%
hc_yAxis(type = "logarithmic", gridLineWidth = 0) %>%
hc_title(text = "Our nearest Stars") %>%
hc_subtitle(text = "In a Hertzsprung-Russell diagram") %>%
hc_tooltip(useHTML = TRUE, headerFormat = "", pointFormat = tltip) %>%
hc_size(height = 600)
data("globaltemp")
x <- c("Min", "Median", "Max")
y <- sprintf("{point.%s}", c("lower", "median", "upper"))
tltip <- tooltip_table(x, y)
hchart(globaltemp, type = "columnrange",
hcaes(x = date, low = lower, high = upper, color = median)) %>%
hc_yAxis(tickPositions = c(-2, 0, 1.5, 2),
gridLineColor = "#B71C1C",
labels = list(format = "{value} C", useHTML = TRUE)) %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = as.character(tags$small("{point.x: %Y %b}")),
pointFormat = tltip
) %>%
hc_add_theme(hc_theme_db())
From here and replicated in http://jkunst.com/r/how-to-weather-radials/
data("weather")
x <- c("Min", "Mean", "Max")
y <- sprintf("{point.%s}", c("min_temperaturec", "mean_temperaturec", "max_temperaturec"))
tltip <- tooltip_table(x, y)
hchart(weather, type = "columnrange",
hcaes(x = date, low = min_temperaturec, high = max_temperaturec,
color = mean_temperaturec)) %>%
hc_chart(polar = TRUE) %>%
hc_yAxis( max = 30, min = -10, labels = list(format = "{value} C"),
showFirstLabel = FALSE) %>%
hc_xAxis(
title = list(text = ""), gridLineWidth = 0.5,
labels = list(format = "{value: %b}")) %>%
hc_tooltip(useHTML = TRUE, pointFormat = tltip,
headerFormat = as.character(tags$small("{point.x:%d %B, %Y}")))
From WSJ graphic: Battling Infectious Diseases in the 20th Century:
data("vaccines")
library("viridis")
Loading required package: viridisLite
fntltp <- JS("function(){
return this.point.x + ' ' + this.series.yAxis.categories[this.point.y] + ':<br>' +
Highcharts.numberFormat(this.point.value, 2);
}")
plotline <- list(
color = "#fde725", value = 1963, width = 2, zIndex = 5,
label = list(
text = "Vaccine Intoduced", verticalAlign = "top",
style = list(color = "#606060"), textAlign = "left",
rotation = 0, y = -5)
)
hchart(vaccines, "heatmap", hcaes(x = year, y = state, value = count)) %>%
hc_colorAxis(stops = color_stops(10, rev(inferno(10))),
type = "logarithmic") %>%
hc_yAxis(reversed = TRUE, offset = -20, tickLength = 0,
gridLineWidth = 0, minorGridLineWidth = 0,
labels = list(style = list(fontSize = "8px"))) %>%
hc_tooltip(formatter = fntltp) %>%
hc_xAxis(plotLines = list(plotline)) %>%
hc_title(text = "Infectious Diseases and Vaccines") %>%
hc_legend(layout = "vertical", verticalAlign = "top",
align = "right", valueDecimals = 0) %>%
hc_size(height = 800)
data(diamonds, package = "ggplot2")
hcboxplot(x = diamonds$x, var = diamonds$color,
name = "Length", color = "#2980b9")
hcboxplot(x = diamonds$x, var = diamonds$color, var2 = diamonds$cut,
outliers = FALSE) %>%
hc_chart(type = "column") # to put box vertical
require(viridisLite)
n <- 15
#hcparcords(head(mtcars, n), color = hex_to_rgba(magma(n), 0.5))
set.seed(123)
icons <- c("motorcycle", "taxi", "bus", "plane")
n <- sample(3:10, length(icons)) %>%
sort(decreasing = TRUE) %>%
{. * seq(length(icons), 1) }
hciconarray(icons, n, icons = icons, size = 5)
Here we use the treemap package to create a treemap object and then we create the same treemap via highcharts ;).
library(treemap)
library(viridisLite)
data(GNI2014)
GNI2014%>%head()
#install.packages("treemap")
library(treemap)
library(viridisLite)
data(GNI2014)
tm <- treemap(GNI2014, index = c("continent", "iso3"),
vSize = "population", vColor = "GNI",
type = "value", palette = viridis(6))
hctreemap(tm)
hc <- highcharts_demo()
hc
hc %>% hc_add_theme(hc_theme_538())
hc %>% hc_add_theme(hc_theme_economist())
hc %>% hc_add_theme(hc_theme_ft())
hc %>% hc_add_theme(hc_theme_db())
hc %>% hc_add_theme(hc_theme_flat())
hc %>% hc_add_theme(hc_theme_flatdark())
hc %>% hc_add_theme(hc_theme_smpl())
hc %>% hc_add_theme(hc_theme_elementary())
hc %>% hc_add_theme(hc_theme_google())
hc %>% hc_add_theme(hc_theme_ffx())
hc %>% hc_add_theme(hc_theme_monokai())
hc %>% hc_add_theme(hc_theme_gridlight())
hc %>% hc_add_theme(hc_theme_sandsignika())
hc %>% hc_add_theme(hc_theme_darkunica())
hc %>% hc_add_theme(hc_theme_chalk())
hc %>% hc_add_theme(hc_theme_handdrawn())
hc %>% hc_add_theme(hc_theme_null())
n <- 15
dta <- dplyr::data_frame(
x = rnorm(n),
y = 1.5 * x + rnorm(n))
highchart() %>%
hc_chart(type = "scatter") %>%
hc_add_series(data = list_parse(dta)) %>%
hc_add_theme(hc_theme_tufte())
values <- 1 + abs(rnorm(12))
highchart() %>%
hc_chart(type = "column") %>%
hc_add_series(data = values) %>%
hc_xAxis(categories = month.abb) %>%
hc_add_theme(hc_theme_tufte2())
# You can test:
# hc %>% hc_add_theme(hc_theme_sparkline())
data(economics_long, package = "ggplot2")
library(dplyr)
economics_long %>%
group_by(variable) %>%
do(spark = hcts(.$value, type = "area",
name = first(.$variable), showInLegend = FALSE) %>%
hc_add_theme(hc_theme_sparkline())) %>%
.[["spark"]] %>%
as.list() %>%
hw_grid(rowheight = 100)
thm <- hc_theme(
colors = c('red', 'green', 'blue'),
chart = list(
backgroundColor = NULL,
divBackgroundImage = "http://media3.giphy.com/media/FzxkWdiYp5YFW/giphy.gif"
),
title = list(
style = list(
color = '#333333',
fontFamily = "Lato"
)
),
subtitle = list(
style = list(
color = '#666666',
fontFamily = "Shadows Into Light"
)
),
legend = list(
itemStyle = list(
fontFamily = 'Tangerine',
color = 'black'
),
itemHoverStyle = list(
color = 'gray'
)
)
)
hc %>% hc_add_theme(thm)
thm <- hc_theme_merge(
hc_theme_darkunica(),
hc_theme(
chart = list(
backgroundColor = "transparent",
divBackgroundImage = "http://cdn.wall-pix.net/albums/art-3Dview/00025095.jpg"
),
title = list(
style = list(
color = 'white',
fontFamily = "Open Sans"
)
)
)
)
hc %>% hc_add_theme(thm)
Basics · Candlestick and OHLC charts · Time series · Flags · A More Interesting Example Basics Highstock work well with the quantmod package. It’s easy chart symbols. And then you can add more series using hc_add_series (see below).
library("quantmod")
x <- getSymbols("GOOG", auto.assign = FALSE)
hchart(x)
Candlestick and OHLC charts If you want to chart more symbols in you can use the hc_add_series function.
x <- getSymbols("GOOG", auto.assign = FALSE)
y <- getSymbols("AMZN", auto.assign = FALSE)
highchart(type = "stock") %>%
hc_add_series(x) %>%
hc_add_series(y, type = "ohlc")
library("quantmod")
usdjpy <- getSymbols("USD/JPY", src = "oanda", auto.assign = FALSE)
eurkpw <- getSymbols("EUR/KPW", src = "oanda", auto.assign = FALSE)
hc <- highchart(type = "stock") %>%
hc_title(text = "Charting some Symbols") %>%
hc_subtitle(text = "Data extracted using quantmod package") %>%
hc_add_series(usdjpy, id = "usdjpy") %>%
hc_add_series(eurkpw, id = "eurkpw")
hc
Previously we used the id parameter. This is necessary to add flags:
library(dplyr)
set.seed(123)
data_flags <- data_frame(
date = sample(time(usdjpy), size = 5),
title = sprintf("E #%s", seq_along(date)),
text = sprintf("An interesting event #%s in %s", seq_along(date), date)
)
glimpse(data_flags)
Observations: 5
Variables: 3
$ date <date> 2017-04-28, 2017-07-27, 2017-05-19, 2017-08-11, 2017-...
$ title <chr> "E #1", "E #2", "E #3", "E #4", "E #5"
$ text <chr> "An interesting event #1 in 2017-04-28", "An interesti...
## Observations: 5
## Variables: 3
## $ date <date> 2016-02-06, 2016-10-12, 2016-04-06, 2016-11-26, 2016-12-24
## $ title <chr> "E #1", "E #2", "E #3", "E #4", "E #5"
## $ text <chr> "An interesting event #1 in 2016-02-06", "An interesting...
hc %>%
hc_add_series(data_flags, hcaes(x = date),
type = "flags", onSeries = "usdjpy")
SPY <- getSymbols("SPY", from = Sys.Date() - lubridate::years(1), auto.assign = FALSE)
SPY <- adjustOHLC(SPY)
SPY.SMA.10 <- SMA(Cl(SPY), n = 5)
SPY.SMA.200 <- SMA(Cl(SPY), n = 100)
SPY.RSI.14 <- RSI(Cl(SPY))
SPY.RSI.SellLevel <- xts(rep(70, NROW(SPY)), index(SPY))
SPY.RSI.BuyLevel <- xts(rep(30, NROW(SPY)), index(SPY))
highchart(type = "stock") %>%
# create axis :)
hc_yAxis_multiples(
create_yaxis(3, height = c(2, 1, 1), turnopposite = TRUE)
) %>%
# series :D
hc_add_series(SPY, yAxis = 0, name = "SPY") %>%
hc_add_series(SPY.SMA.10, yAxis = 0, name = "Fast MA") %>%
hc_add_series(SPY.SMA.200, yAxis = 0, name = "Slow MA") %>%
hc_add_series(SPY$SPY.Volume, color = "gray", yAxis = 1, name = "Volume", type = "column") %>%
hc_add_series(SPY.RSI.14, yAxis = 2, name = "Osciallator", color = hex_to_rgba("green", 0.7)) %>%
hc_add_series(SPY.RSI.SellLevel, color = hex_to_rgba("red", 0.7),
yAxis = 2, name = "Sell level") %>%
hc_add_series(SPY.RSI.BuyLevel, color = hex_to_rgba("blue", 0.7),
yAxis = 2, name = "Buy level")
Basics · Choropleths · Adding More Data · Advanced Maps · Motion Plugin · geojsonio Package Basics The easiest way to chart a map with highcharter is using hcmap function. Select a map (a url) from the highmaps collection https://code.highcharts.com/mapdata/. and use the url as a map in hcmap function. This will download the map and create a object using the info as a mapData argument.
What about add data to get a choropleth? Every map downloaded from the highcharts maps collection have keys to join data. There are 2 functions to help to know what are the regions coded to know how to join the map and data:
download_map_data: Download the geojson data from the highcharts collection. get_data_from_map: Get the properties for each region in the map, as the keys from the map data.
library(dplyr)
mapdata <- get_data_from_map(download_map_data("countries/us/us-all"))
trying URL 'https://code.highcharts.com/mapdata/countries/us/us-all.js'
Content type 'application/x-javascript' length 66923 bytes (65 KB)
==================================================
downloaded 65 KB
glimpse(mapdata)
Observations: 52
Variables: 19
$ `hc-group` <chr> "admin1", "admin1", "admin1", "admin1", "admin...
$ `hc-middle-x` <dbl> 0.36, 0.56, 0.51, 0.47, 0.41, 0.43, 0.71, 0.46...
$ `hc-middle-y` <dbl> 0.47, 0.52, 0.67, 0.52, 0.38, 0.40, 0.67, 0.38...
$ `hc-key` <chr> "us-ma", "us-wa", "us-ca", "us-or", "us-wi", "...
$ `hc-a2` <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV"...
$ labelrank <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0", "...
$ hasc <chr> "US.MA", "US.WA", "US.CA", "US.OR", "US.WI", "...
$ `woe-id` <chr> "2347580", "2347606", "2347563", "2347596", "2...
$ `state-fips` <chr> "25", "53", "6", "41", "55", "23", "26", "32",...
$ fips <chr> "US25", "US53", "US06", "US41", "US55", "US23"...
$ `postal-code` <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV"...
$ name <chr> "Massachusetts", "Washington", "California", "...
$ country <chr> "United States of America", "United States of ...
$ region <chr> "Northeast", "West", "West", "West", "Midwest"...
$ longitude <chr> "-71.99930000000001", "-120.361", "-119.591", ...
$ `woe-name` <chr> "Massachusetts", "Washington", "California", "...
$ latitude <chr> "42.3739", "47.4865", "36.7496", "43.8333", "4...
$ `woe-label` <chr> "Massachusetts, US, United States", "Washingto...
$ type <chr> "State", "State", "State", "State", "State", "...
## Observations: 52
## Variables: 19
## $ hc-group <chr> "admin1", "admin1", "admin1", "admin1", "admin1", ...
## $ hc-middle-x <dbl> 0.36, 0.56, 0.51, 0.47, 0.41, 0.43, 0.71, 0.46, 0....
## $ hc-middle-y <dbl> 0.47, 0.52, 0.67, 0.52, 0.38, 0.40, 0.67, 0.38, 0....
## $ hc-key <chr> "us-ma", "us-wa", "us-ca", "us-or", "us-wi", "us-m...
## $ hc-a2 <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "N...
## $ labelrank <chr> "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", ...
## $ hasc <chr> "US.MA", "US.WA", "US.CA", "US.OR", "US.WI", "US.M...
## $ woe-id <chr> "2347580", "2347606", "2347563", "2347596", "23476...
## $ state-fips <chr> "25", "53", "6", "41", "55", "23", "26", "32", "35...
## $ fips <chr> "US25", "US53", "US06", "US41", "US55", "US23", "U...
## $ postal-code <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "N...
## $ name <chr> "Massachusetts", "Washington", "California", "Oreg...
## $ country <chr> "United States of America", "United States of Amer...
## $ region <chr> "Northeast", "West", "West", "West", "Midwest", "N...
## $ longitude <chr> "-71.99930000000001", "-120.361", "-119.591", "-12...
## $ woe-name <chr> "Massachusetts", "Washington", "California", "Oreg...
## $ latitude <chr> "42.3739", "47.4865", "36.7496", "43.8333", "44.37...
## $ woe-label <chr> "Massachusetts, US, United States", "Washington, U...
## $ type <chr> "State", "State", "State", "State", "State", "Stat...
set.seed(1234)
data_fake <- mapdata %>%
select(code = `hc-a2`) %>%
mutate(value = 1e5 * abs(rt(nrow(.), df = 10)))
glimpse(data_fake)
Observations: 52
Variables: 2
$ code <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "NM", ...
$ value <dbl> 119426.518, 255662.317, 3668.575, 122246.930, 79283.06...
## Observations: 52
## Variables: 2
## $ code <chr> "MA", "WA", "CA", "OR", "WI", "ME", "MI", "NV", "NM", "C...
## $ value <dbl> 119426.518, 255662.317, 3668.575, 122246.930, 79283.064,...
hcmap("countries/us/us-all", data = data_fake, value = "value",
joinBy = c("hc-a2", "code"), name = "Fake data",
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 2, valuePrefix = "$", valueSuffix = " USD"))
trying URL 'https://code.highcharts.com/mapdata/countries/us/us-all.js'
Content type 'application/x-javascript' length 66923 bytes (65 KB)
==================================================
downloaded 65 KB
library(highcharter)
library(dplyr)
library(purrr)
set.seed(1234)
n <- 20
z <- sample(1:n)
sequences <- map2(1:n, z, function(x, y){ ifelse(x == 1:n, y, 0) })
df <- data_frame(
lat = runif(n, -180, 180),
lon = runif(n, -180, 180),
z = z,
color = colorize(z),
sequence = sequences
)
hcmap() %>%
hc_add_series(data = df, type = "mapbubble",
minSize = 0, maxSize = 30) %>%
hc_motion(enabled = TRUE, series = 1, labels = 1:n,
loop = TRUE, autoPlay = TRUE,
updateInterval = 1000, magnet = list(step = 1)) %>%
hc_plotOptions(series = list(showInLegend = FALSE))
trying URL 'https://code.highcharts.com/mapdata/custom/world.js'
Content type 'application/x-javascript' length 190328 bytes (185 KB)
==================================================
downloaded 185 KB
hcmap("countries/us/us-ca-all") %>%
hc_title(text = "California")
trying URL 'https://code.highcharts.com/mapdata/countries/us/us-ca-all.js'
Content type 'application/x-javascript' length 40824 bytes (39 KB)
==================================================
downloaded 39 KB
hcmap("custom/usa-and-canada", showInLegend = FALSE)
trying URL 'https://code.highcharts.com/mapdata/custom/usa-and-canada.js'
Content type 'application/x-javascript' length 134466 bytes (131 KB)
==================================================
downloaded 131 KB
hcmap("countries/nz/nz-all")
trying URL 'https://code.highcharts.com/mapdata/countries/nz/nz-all.js'
Content type 'application/x-javascript' length 36810 bytes (35 KB)
==================================================
downloaded 35 KB
highchart() %>%
hc_chart(type = "column") %>%
hc_yAxis(max = 6, min = 0) %>%
hc_add_series(name = "A", data = c(2,3,4), zIndex = -10) %>%
hc_add_series(name = "B",
data = list(
list(sequence = c(1,2,3,4)),
list(sequence = c(3,2,1,3)),
list(sequence = c(2,5,4,3))
)) %>%
hc_add_series(name = "C",
data = list(
list(sequence = c(3,2,1,3)),
list(sequence = c(2,5,4,3)),
list(sequence = c(1,2,3,4))
)) %>%
hc_motion(enabled = TRUE,
labels = 2000:2003,
series = c(1,2))
install.packages("idbr")
Installing package into ‘/Users/nanaakwasiabayieboateng/Library/R/3.4/library’
(as ‘lib’ is unspecified)
also installing the dependency ‘countrycode’
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/countrycode_0.19.tgz'
Content type 'application/x-gzip' length 47143 bytes (46 KB)
==================================================
downloaded 46 KB
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/idbr_0.2.tgz'
Content type 'application/x-gzip' length 27358 bytes (26 KB)
==================================================
downloaded 26 KB
The downloaded binary packages are in
/var/folders/mj/w1gxzjcd0qx2cw_0690z7y640000gn/T//RtmpNTeDp4/downloaded_packages
library("idbr")
library("purrr")
library("dplyr")
idb_api_key("35f116582d5a89d11a47c7ffbfc2ba309133f09d")
yrs <- seq(1980, 2030, by = 5)
df <- map_df(c("male", "female"), function(sex){
mutate(idb1("US", yrs, sex = sex), sex_label = sex)
})
names(df) <- tolower(names(df))
df <- df %>%
mutate(population = pop*ifelse(sex_label == "male", -1, 1))
series <- df %>%
group_by(sex_label, age) %>%
do(data = list(sequence = .$population)) %>%
ungroup() %>%
group_by(sex_label) %>%
do(data = .$data) %>%
mutate(name = sex_label) %>%
list_parse()
maxpop <- max(abs(df$population))
xaxis <- list(categories = sort(unique(df$age)),
reversed = FALSE, tickInterval = 5,
labels = list(step = 5))
highchart() %>%
hc_chart(type = "bar") %>%
hc_motion(enabled = TRUE, labels = yrs, series = c(0,1), autoplay = TRUE, updateInterval = 1) %>%
hc_add_series_list(series) %>%
hc_plotOptions(
series = list(stacking = "normal"),
bar = list(groupPadding = 0, pointPadding = 0, borderWidth = 0)
) %>%
hc_tooltip(shared = TRUE) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) / 1000000 + 'M'; }")
),
tickInterval = 0.5e6,
min = -maxpop,
max = maxpop) %>%
hc_xAxis(
xaxis,
rlist::list.merge(xaxis, list(opposite = TRUE, linkedTo = 0))
) %>%
hc_tooltip(shared = FALSE,
formatter = JS("function () { return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + 'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);}")
)
data("citytemp")
highchart() %>%
hc_chart(animation = FALSE) %>%
hc_title(text = "draggable points demo") %>%
hc_subtitle(text = "Drang my points plz") %>%
hc_xAxis(categories = month.abb) %>%
hc_plotOptions(
series = list(
point = list(
events = list(
drop = JS("function(){
alert(this.series.name + ' ' + this.category + ' ' + Highcharts.numberFormat(this.y, 2))
}")
)
),
stickyTracking = FALSE
),
column = list(
stacking = "normal"
),
line = list(
cursor = "ns-resize"
)
) %>%
hc_tooltip(yDecimals = 2) %>%
hc_add_series(
data = citytemp$tokyo,
draggableY = TRUE,
dragMinY = 0,
type = "column",
minPointLength = 2
) %>%
hc_add_series(
data = citytemp$new_york,
draggableY = TRUE,
dragMinY = 0,
type = "column",
minPointLength = 2
) %>%
hc_add_series(
data = citytemp$berlin,
draggableY = TRUE
)
library(purrr) # map function to make grouped categories argument
library(dplyr) # for select function
data(mpg, package = "ggplot2")
mpgg <- mpg %>%
filter(class %in% c("suv", "compact", "midsize")) %>%
group_by(class, manufacturer) %>%
summarize(count = n())
categories_grouped <- mpgg %>%
group_by(name = class) %>%
do(categories = .$manufacturer) %>%
list_parse()
highchart() %>%
hc_xAxis(categories = categories_grouped) %>%
hc_add_series(data = mpgg, type = "bar", hcaes(y = count, color = manufacturer),
showInLegend = FALSE)
data(mpg, package = "ggplot2")
mpgman <- mpg %>%
group_by(manufacturer) %>%
summarise(n = n(),
unique = length(unique(model))) %>%
arrange(-n, -unique)
head(mpgman)
diamonds%>%head()
highchart() %>%
hc_add_series(data=diamonds, type = "scatter",
hcaes(x = carat, y = price, size = depth, group = cut))
|============================ | 60% ~2 s remaining
|===================================== | 80% ~1 s remaining
|===============================================|100% ~0 s remaining