In their words:

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.

rbokeh is one of my new favourite R things.

I never used bokeh before, but I am a big fan of the R version. rbokeh is now my favourite way of quickly visualizing data while doing first pass or exploratory analyses. When I think “I wonder what this data looks like?”, I jump to rbokeh first.

The syntax is efficient and intuitive (maybe because I am already familiar with ggplot) and I feel I get a lot of great stuff for little effort.

Want the ability to see values when you hover over points? Just add hover = list(var1, var2). Want to be able to lasso a group of points on a plot and focus on those? Just add %>% tool_lasso_select().

Since rbokeh creates plots using HTML canvas, it’s well-suited to plotting in R Markdown documents and embedding plots in web pages like this one.


The following is a summary or repetition of what I learned from the rbokeh documentation and this post by Bob Rudis. Why the repetition? In the spirit of “two is one and one is none”, and because it never hurts to have redundancy for knowledge on the internet.

To plot in R / RStudio

You need to install and load the rbokeh library.


The documentation is a good introduction to the basic plots you can create. Here is a starter example:


figure() %>% 
  ly_points(x = Sepal.Length, y = Petal.Length, data = iris)

The amount of plot and functionality I get per keystroke still gives me a rush.

That was a basic plot. Let’s do some customization.


figure(legend_location = "top_left", outline_line_alpha = 0) %>% 
  ly_points(x = Sepal.Length, y = Petal.Length, data = iris,
           glyph = Species,
           hover = list(Sepal.Length, Petal.Length)) %>% 
  ly_lines(lowess(iris$Sepal.Length, iris$Petal.Length), color = "red", legend = "Lowess line") %>% 
  x_axis(label = "Sepal length", grid = FALSE) %>% 
  y_axis(label = "Petal length", grid = FALSE) %>% 

To save the plot to an html file

You will need the htmlwidgets library. From that, you can use the saveWidget() function.


myplot <- figure() %>% 
              ly_points(x = Sepal.Length, y = Petal.Length, data = iris)

saveWidget(myplot, file = "iris_plot.html")

To embed plot in Jekyll or other web pages

The best way I’ve found is to use an iframe.

<iframe src="/assets/plots/iris_plot.html"
    style="max-width = 100%"
    sandbox="allow-same-origin allow-scripts"

You can control the plot dimensions from the robokeh end if you need to:

figure(width = 500, height = 600) %>% 
  ly_points(x = Sepal.Length, y = Petal.Length, data = iris)

There is one downside to embedding rbokeh plots in web pages: they are expensive when it comes to bandwidth.

The two plots on this page are 1.3 MB each. That’s a lot. This page is 2.62 MB in total. The rbokeh plots make up 99% of that. So you can’t throw these plots around willy-nilly. Use few and make them count.

Actually, let me be that guy for a second: “use few and make them count” is good advice for plots of any kind.