Bokeh is a visualization library that provides a flexible and powerful declarative framework for creating web-based plots. Bokeh renders plots using HTML canvas and provides many mechanisms for interactivity. Bokeh has interfaces in Python, Scala, Julia, and now R.
The Bokeh library is written and maintained by the Bokeh Core Team consisting of several members of Continuum Analytics and other members of the open source community. The rbokeh package is written and maintained by Ryan Hafen (@hafenstats) with several contributions from others. Contributions are welcome.
The rbokeh package can be installed from CRAN:
library(rbokeh) #> Registered S3 method overwritten by 'pryr': #> method from #> print.bytes Rcpp
Plots are constructed by initializing a
figure() and then adding layers on top through various glyphs available in Bokeh, or abstractions of those glyphs that we have created for common use cases. The data input is typically
y, and how they are specified is quite flexible (see examples below).
Before providing a tutorial, we first show several examples of plots created with rbokeh. This will both give a feel for what the syntax looks like and provide some motivation to go through the more procedural tutorial. We thought this would be a more enjoyable way to begin than looking at 50 different versions of a plot of the iris data with different parameter settings.
Speaking of the iris data, our first plot:
Here since we are specifying color and glyph by
Species, a legend is automatically created and the points are colored according to the default color scheme. You can hover over points to see the tooltips we added with the
hover argument. You can also play around with the other interactive components such as panning and zooming.
We can also specify legend entries manually:
Histogram of old faithful geyser data with density overplotted:
h <- figure(width = 600, height = 400) %>% ly_hist(eruptions, data = faithful, breaks = 40, freq = FALSE) %>% ly_density(eruptions, data = faithful) h
Periodic table of the elements with additional info on hover:
# prepare data elements <- subset(elements, !is.na(group)) elements$group <- as.character(elements$group) elements$period <- as.character(elements$period) # add colors for groups metals <- c("alkali metal", "alkaline earth metal", "halogen", "metal", "metalloid", "noble gas", "nonmetal", "transition metal") colors <- c("#a6cee3", "#1f78b4", "#fdbf6f", "#b2df8a", "#33a02c", "#bbbb88", "#baa2a6", "#e08e79") elements$color <- colors[match(elements$metal, metals)] elements$type <- elements$metal # make coordinates for labels elements$symx <- paste(elements$group, ":0.1", sep = "") elements$numbery <- paste(elements$period, ":0.8", sep = "") elements$massy <- paste(elements$period, ":0.15", sep = "") elements$namey <- paste(elements$period, ":0.3", sep = "") # create figure p <- figure(title = "Periodic Table", tools = c("resize", "hover"), ylim = as.character(c(7:1)), xlim = as.character(1:18), xgrid = FALSE, ygrid = FALSE, xlab = "", ylab = "", height = 445, width = 800) %>% # plot rectangles ly_crect(group, period, data = elements, 0.9, 0.9, fill_color = color, line_color = color, fill_alpha = 0.6, hover = list(name, atomic.number, type, atomic.mass, electronic.configuration)) %>% # add symbol text ly_text(symx, period, text = symbol, data = elements, font_style = "bold", font_size = "10pt", align = "left", baseline = "middle") %>% # add atomic number text ly_text(symx, numbery, text = atomic.number, data = elements, font_size = "6pt", align = "left", baseline = "middle") %>% # add name text ly_text(symx, namey, text = name, data = elements, font_size = "4pt", align = "left", baseline = "middle") %>% # add atomic mass text ly_text(symx, massy, text = atomic.mass, data = elements, font_size = "4pt", align = "left", baseline = "middle") p
Crude map of the world with capital cities:
library(maps) data(world.cities) caps <- subset(world.cities, capital == 1) caps$population <- prettyNum(caps$pop, big.mark = ",") figure(width = 800, height = 450, padding_factor = 0) %>% ly_map("world", col = "gray") %>% ly_points(long, lat, data = caps, size = 5, hover = c(name, country.etc, population))