We chart data. Data can come in different ways: numeric or character vectors, as time series objects, etc. but the most common object with data is a data frame. So, why can chart this type of object in highcharter?
Highcharter have two main functions to create a chart from data and another to add data to an existing highchart
object.
hchart
: A generic function which take an object (like vector, time series, data frames, likert object, etc) and return a highchart
object (chart)hc_add_series
: A generic function which add data to a existing highchart
object depending the type (class) of the data.There are a last function will be useful to chart data from data frame. The functions is hcaes
which will define the aesthetic mappings. This 3 functions are inspired in ggplot2 package. So:
hchart
works like ggplot2’s qplot
.hc_add_series
works like ggplot2’s geom_
s.hcaes
works like ggplot2’s aes
.The main differences with ggplot2 are here we need the data and the aesthetics explicit in every highchart functions. Lets see example to be more clear.
data("mpg", package = "ggplot2")
head(mpg)
## # A tibble: 6 × 11
## manufacturer model displ year cyl trans drv cty hwy fl
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p
## 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p
## 4 audi a4 2.0 2008 4 auto(av) f 21 30 p
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p
## # ... with 1 more variables: class <chr>
hchart(mpg, "point", hcaes(x = displ, y = cty))
The previous code is same as:
highchart() %>%
hc_add_series(mpg, "point", hcaes(x = displ, y = cty))
Other example can be:
library(dplyr)
library(broom)
data(diamonds, package = "ggplot2")
set.seed(123)
data <- sample_n(diamonds, 300)
modlss <- loess(price ~ carat, data = data)
fit <- arrange(augment(modlss), carat)
head(fit)
## price carat .fitted .se.fit .resid
## 1 402 0.23 418.2802 346.1437 -16.28023
## 2 552 0.24 447.5185 322.9975 104.48150
## 3 740 0.25 477.5461 300.9198 262.45388
## 4 627 0.27 540.0385 260.3266 86.96146
## 5 449 0.27 540.0385 260.3266 -91.03854
## 6 641 0.27 540.0385 260.3266 100.96146
highchart() %>%
hc_add_series(data, type = "scatter",
hcaes(x = carat, y = price, size = depth, group = cut)) %>%
hc_add_series(fit, type = "spline", hcaes(x = carat, y = .fitted),
name = "Fit", id = "fit") %>%
hc_add_series(fit, type = "arearange",
hcaes(x = carat, low = .fitted - 2*.se.fit,
high = .fitted + 2*.se.fit),
linkedTo = "fit")
With highcharter you can have other type of charts.
dfdiam <- diamonds %>%
group_by(cut, clarity) %>%
summarize(price = median(price))
hchart(dfdiam, "heatmap", hcaes(x = cut, y = clarity, value = price))
data(economics_long, package = "ggplot2")
economics_long2 <- filter(economics_long,
variable %in% c("pop", "uempmed", "unemploy"))
hchart(economics_long2, "line", hcaes(x = date, y = value01, group = variable))
You can even chart a treemaps:
data(mpg, package = "ggplot2")
mpgman <- mpg %>%
group_by(manufacturer) %>%
summarise(n = n(),
unique = length(unique(model))) %>%
arrange(-n, -unique)
head(mpgman)
## # A tibble: 6 × 3
## manufacturer n unique
## <chr> <int> <int>
## 1 dodge 37 4
## 2 toyota 34 6
## 3 volkswagen 27 4
## 4 ford 25 4
## 5 chevrolet 19 4
## 6 audi 18 3
hchart(mpgman, "treemap", hcaes(x = manufacturer, value = n, color = unique))
You can add other parameters to add options to the data series:
mpgman2 <- count(mpg, manufacturer, year)
hchart(mpgman2, "bar", hcaes(x = manufacturer, y = n, group = year),
color = c("#FCA50A", "#FCFFA4"),
name = c("year 1999", "Year 2008"))