Highcharts is a SVG-based, multi-platform charting library. The highcharter package bring all highchartsJS capabilities to your R console. So you can chart from simple charts like column, scatter to more complex charts like streamgraph, packed bubble, vector field among others.
Basic charts
Let’s see a preview of what highchartsJS (so highcharter) can chart. First of all a simple data set:
x | y | z | low | high | value | name | color | from | to | weight |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1.6 | -34.0 | -6.0 | 9.2 | 1 | lemon | #d35400 | lemon | olive | 1 |
1 | 11.0 | -23.0 | 6.7 | 15.3 | 10 | nut | #2980b9 | lemon | guava | 1 |
2 | 20.4 | 6.8 | 2.8 | 38.0 | 19 | olive | #2ecc71 | lemon | fig | 1 |
3 | 22.1 | 32.3 | 19.4 | 24.8 | 21 | guava | #f1c40f | nut | olive | 1 |
4 | 15.4 | 27.7 | 12.1 | 18.7 | 14 | fig | #2c3e50 | olive | pear | 2 |
5 | 7.4 | 3.2 | -11.8 | 26.6 | 6 | pear | #7f8c8d | guava | pear | 2 |
xrange
library(lubridate)
N <- 7
set.seed(1234)
df <- tibble(
start = Sys.Date() + months(sample(10:20, size = N)),
end = start + months(sample(1:3, size = N, replace = TRUE)),
cat = rep(1:5, length.out = N) - 1,
progress = round(stats::runif(N), 1)
)
df <- mutate_if(df, is.Date, datetime_to_timestamp)
hchart(
df,
"xrange",
hcaes(x = start, x2 = end, y = cat, partialFill = progress),
dataLabels = list(enabled = TRUE)
) %>%
hc_xAxis(
title = FALSE,
type = "datetime"
) %>%
hc_yAxis(
title = FALSE,
categories = c("Prototyping", "Development", "Testing", "Validation", "Modelling")
)
vector
x <- seq(5, 95, by = 5)
df <- expand.grid(x = x, y = x) %>%
mutate(
length = 200 - (x + y),
direction = (x + y)/200 * 360
)
glimpse(df)
## Rows: 361
## Columns: 4
## $ x <dbl> 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 8~
## $ y <dbl> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,~
## $ length <dbl> 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, ~
## $ direction <dbl> 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135, ~
tilemap
# example 1
# http://www.maartenlambrechts.com/2017/10/22/tutorial-a-worldtilegrid-with-ggplot2.html
library(tidyverse)
url <- "https://gist.githubusercontent.com/maartenzam/787498bbc07ae06b637447dbd430ea0a/raw/9a9dafafb44d8990f85243a9c7ca349acd3a0d07/worldtilegrid.csv"
data <- read_csv(url)
data <- data %>%
rename_all(str_replace_all, "\\.", "_") %>%
select(x, y, name, region, alpha_2)
glimpse(data)
## Rows: 192
## Columns: 5
## $ x <dbl> 22, 15, 13, 13, 15, 7, 6, 20, 24, 15, 21, 4, 20, 23, 8, 17, 13~
## $ y <dbl> 8, 9, 11, 17, 23, 4, 14, 6, 19, 6, 7, 2, 9, 8, 6, 4, 5, 3, 14,~
## $ name <chr> "Afghanistan", "Albania", "Algeria", "Angola", "Antarctica", "~
## $ region <chr> "Asia", "Europe", "Africa", "Africa", "Antarctica", "Americas"~
## $ alpha_2 <chr> "AF", "AL", "DZ", "AO", "AQ", "AG", "AR", "AM", "AU", "AT", "A~
hchart(data, "tilemap", hcaes(x = x, y = -y, name = name, group = region)) %>%
hc_chart(type = "tilemap") %>%
hc_plotOptions(
series = list(
dataLabels = list(
enabled = TRUE,
format = "{point.alpha_2}",
color = "white",
style = list(textOutline = FALSE)
)
)
) %>%
hc_tooltip(
headerFormat = "",
pointFormat = "<b>{point.name}</b> is in <b>{point.region}</b>"
) %>%
hc_xAxis(visible = FALSE) %>%
hc_yAxis(visible = FALSE) %>%
hc_size(height = 800)
packedbubble
library(tidyverse)
data(gapminder, package = "gapminder")
gapminder <- gapminder %>%
filter(year == max(year)) %>%
select(country, pop, continent)
hc <- hchart(gapminder, "packedbubble", hcaes(name = country, value = pop, group = continent))
q95 <- as.numeric(quantile(gapminder$pop, .95))
hc %>%
hc_tooltip(
useHTML = TRUE,
pointFormat = "<b>{point.name}:</b> {point.value}"
) %>%
hc_plotOptions(
packedbubble = list(
maxSize = "150%",
zMin = 0,
layoutAlgorithm = list(
gravitationalConstant = 0.05,
splitSeries = TRUE, # TRUE to group points
seriesInteraction = TRUE,
dragBetweenSeries = TRUE,
parentNodeLimit = TRUE
),
dataLabels = list(
enabled = TRUE,
format = "{point.name}",
filter = list(
property = "y",
operator = ">",
value = q95
),
style = list(
color = "black",
textOutline = "none",
fontWeight = "normal"
)
)
)
)
venn & euler
highchart() %>%
hc_chart(type = "venn") %>%
hc_add_series(
dataLabels = list(style = list(fontSize = "20px")),
name = "Venn Diagram",
data = list(
list(
name = "People who are<br>breaking my heart.",
sets = list("A"), value = 5
),
list(
name = "People who are shaking<br> my confidence daily.",
sets = list("B"), value = 5
),
list(
name = "Cecilia", sets = list("B", "A"), value = 1)
)
)
highchart() %>%
hc_chart(type = "venn") %>%
hc_add_series(
name = "Euler Diagram",
dataLabels = list(style = list(fontSize = "20px")),
data = list(
list(sets = list("A"), name = "Animals", value = 5),
list(sets = list("B"), name = "Four Legs", value = 1),
list(sets = list("B", "A"), value = 1),
list(sets = list("C"), name = "Mineral", value = 2)
)
)
solidgauge
col_stops <- data.frame(
q = c(0.15, 0.4, .8),
c = c('#55BF3B', '#DDDF0D', '#DF5353'),
stringsAsFactors = FALSE
)
highchart() %>%
hc_chart(type = "solidgauge") %>%
hc_pane(
startAngle = -90,
endAngle = 90,
background = list(
outerRadius = '100%',
innerRadius = '60%',
shape = "arc"
)
) %>%
hc_tooltip(enabled = FALSE) %>%
hc_yAxis(
stops = list_parse2(col_stops),
lineWidth = 0,
minorTickWidth = 0,
tickAmount = 2,
min = 0,
max = 100,
labels = list(y = 26, style = list(fontSize = "22px"))
) %>%
hc_add_series(
data = 90,
dataLabels = list(
y = -50,
borderWidth = 0,
useHTML = TRUE,
style = list(fontSize = "40px")
)
) %>%
hc_size(height = 300)
bullet
df <- data.frame(
y = sample(5:10),
target = sample(5:10),
x = LETTERS[1:6]
)
glimpse(df)
## Rows: 6
## Columns: 3
## $ y <int> 8, 9, 10, 7, 6, 5
## $ target <int> 9, 6, 5, 10, 7, 8
## $ x <chr> "A", "B", "C", "D", "E", "F"
hchart(df, "bullet", hcaes(x = x, y = y, target = target), color = "black") %>%
hc_chart(inverted = TRUE) %>%
hc_yAxis(
min = 0,
max = 10,
gridLineWidth = 0,
plotBands = list(
list(from = 0, to = 7, color = "#666"),
list(from = 7, to = 9, color = "#999"),
list(from = 9, to = 10, color = "#bbb")
)
) %>%
hc_xAxis(
gridLineWidth = 15,
gridLineColor = "white"
) %>%
hc_plotOptions(
series = list(
pointPadding = 0.25,
pointWidth = 15,
borderWidth = 0,
targetOptions = list(width = '200%')
)
) %>%
hc_size(height = 300)