{"id":3214,"date":"2016-02-17T19:25:24","date_gmt":"2016-02-17T18:25:24","guid":{"rendered":"http:\/\/pzd.hmy.temporary.site\/?p=3214"},"modified":"2017-04-28T18:26:23","modified_gmt":"2017-04-28T22:26:23","slug":"interactive-plotting-with-rbokeh","status":"publish","type":"post","link":"https:\/\/datascienceplus.com\/interactive-plotting-with-rbokeh\/","title":{"rendered":"Interactive plotting with rbokeh"},"content":{"rendered":"<p>Hello everyone! In this post, I will show you how you can use <code>rbokeh<\/code> to build interactive graphs and maps in R. <\/p>\n<h2>What is bokeh?<\/h2>\n<p>Bokeh is a popular python library used for building interactive plots and maps, and now it is also available in R, thanks to <a href=\"https:\/\/twitter.com\/hafenstats\" target=\"_blank\">Ryan Hafen<\/a>. It is a very powerful for creating good looking plots for the web easily, and it is fully compatible with shiny.<\/p>\n<p>Generally, plotting in bokeh is done by adding layers to a plot, similar to <code>ggplot2<\/code>. For creating a simple plot, there are two main steps involved:<\/p>\n<ul>\n<li><code>figure()<\/code> &#8211; This will initialize the bokeh plot. It has a variety of parameters to set width, height, title, and axes parameters.<\/li>\n<li><code>ly_geom()<\/code> &#8211; This will specify the type of geom you want to use. There are a variety of options, including <code>ly_points<\/code>, <code>ly_lines<\/code>, <code>ly_hist<\/code>, <code>ly_boxplot<\/code>, etc. Each of these have parameters which allow for specifying size, color, what to show on hover, etc.<\/li>\n<\/ul>\n<p>Okay, let&#8217;s start building some visualizations! Installation instructions are available <a href=\"http:\/\/hafen.github.io\/rbokeh\" target=\"_blank\">here<\/a>.<\/p>\n<p>In one of previous posts, I showed how you can do <a href=\"https:\/\/datascienceplus.com\/hierarchical-clustering-in-r\/\">Hiearchical Clustering in R<\/a>, and demonstrated it with the <code>iris<\/code> dataset. Let&#8217;s recreate the visualization using <code>rbokeh<\/code>:<\/p>\n<pre>clusters &lt;- hclust(dist(iris[, 3:4]), method = &#039;average&#039;)\r\nclusterCut &lt;- cutree(clusters, 3)\r\np &lt;- figure(title = 'Hierarchical Clustering of Iris Data') &#037;&gt;&#037; \r\n  ly_points(Petal.Length, Petal.Width, data = iris, color = Species, hover = c(Sepal.Length, Sepal.Width)) %&gt;%\r\n  ly_points(iris$Petal.Length, iris$Petal.Width, glyph = clusterCut, size = 13)\r\np<\/pre>\n<p>which gives us the following plot:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.05.43-AM.png\" rel=\"attachment wp-att-3249\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.05.43-AM-490x480.png\" alt=\"graph 1\" width=\"490\" height=\"480\" class=\"alignnone size-medium wp-image-3249\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.05.43-AM-490x480.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.05.43-AM-300x294.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.05.43-AM.png 559w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>All the points where the two colors don&#8217;t match are the ones that were clustered in correctly.<\/p>\n<p>Now, let&#8217;s build a chart to show apple stock data for the past year. The data was obtained from <a href=\"http:\/\/finance.yahoo.com\/q\/hp?s=AAPL&amp;a=01&amp;b=15&amp;c=2015&amp;d=01&amp;e=15&amp;f=2016&amp;g=d\" target=\"_blank\">Yahoo Finance<\/a>.<\/p>\n<pre>aapl &lt;- read.csv(&#039;aapl.csv&#039;)\r\naapl$Date &lt;- as.Date(aapl$Date)\r\np &lt;- figure(title = 'Apple Stock Data') &#037;&gt;&#037; \r\n  ly_points(Date, Volume \/ (10 ^ 6), data = aapl, hover = c(Date, High, Open, Close)) %&gt;%\r\n  ly_abline(v = with(aapl, Date[which.max(Volume)])) %&gt;%\r\n  y_axis(label = 'Volume in millions', number_formatter = 'numeral', format = '0.00')<\/pre>\n<p>which gives us the following plot (with a vertical line on the date with the highest amount of volume):<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.47.02-AM.png\" rel=\"attachment wp-att-3254\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.47.02-AM-490x464.png\" alt=\"graph 2\" width=\"490\" height=\"464\" class=\"alignnone size-medium wp-image-3254\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.47.02-AM-490x464.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.47.02-AM-300x284.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-12.47.02-AM.png 555w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>In another previous post, I demonstrated how you can use <a href=\"https:\/\/datascienceplus.com\/building-interactive-maps-with-leaflet\/\">Leaflet to build Interactive Maps<\/a>. Let&#8217;s recreate this using <code>rbokeh<\/code>:<\/p>\n<pre>SFData &lt;- read.csv(&#039;SFPD_Incidents_-_Previous_Year__2015_.csv&#039;)\r\ndata &lt;- subset(SFData, Category == &#039;BRIBERY&#039; | Category == &#039;SUICIDE&#039;)\r\np &lt;- gmap(lat = 37.78, lng = -122.42, zoom = 13) &#037;&gt;&#037;\r\n  ly_points(Y, X, data = data, hover = c(Category, PdDistrict), col = 'red') %&gt;%\r\n  x_axis(visible = FALSE) %&gt;%\r\n  y_axis(visible = FALSE)<\/pre>\n<p>which gives us the following plot:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-1.10.26-AM.png\" rel=\"attachment wp-att-3258\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-1.10.26-AM-490x473.png\" alt=\"graph 3\" width=\"490\" height=\"473\" class=\"alignnone size-medium wp-image-3258\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-1.10.26-AM-490x473.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-1.10.26-AM-300x289.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-1.10.26-AM.png 509w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>We can also somewhat replicate the <code>facet_grid<\/code> feature from <code>ggplot2<\/code> as follows. We will use the <code>diamonds<\/code> dataset from <code>ggplot2<\/code>.<\/p>\n<pre>diamonds &lt;- ggplot2:: diamonds\r\nl &lt;- levels(diamonds$color)\r\nplot_list &lt;- vector(mode = &#039;list&#039;, 7)\r\n\r\nfor (i in 1:length(l)) {\r\n  data &lt;- subset(diamonds, color == l[i])\r\n  plot_list[[i]] &lt;- figure(width = 350, height = 350) &#037;&gt;&#037;\r\n    ly_points(carat, price, data = data, legend = l[i], hover = c(cut, clarity))\r\n}\r\n\r\ngrid_plot(plot_list, nrow = 2)<\/pre>\n<p>which gives us this plot:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM.png\" rel=\"attachment wp-att-3262\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-490x239.png\" alt=\"graph 4\" width=\"490\" height=\"239\" class=\"alignnone size-medium wp-image-3262\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-490x239.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-768x375.png 768w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-1024x500.png 1024w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-600x293.png 600w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM-300x147.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-15-at-6.39.53-PM.png 1437w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>Pretty cool, don&#8217;t you think? If you want to learn more: <a href=\"http:\/\/hafen.github.io\/rbokeh\/\" target=\"_blank\">The official documentation<\/a>. The author explains in detail about more customization options, and also shows you how you can build even cooler visualizations, including a visualization of the periodic table, and a visualization of baseball data to show the density of fielding locations of all doubles.<\/p>\n<p>That brings us to the end of the article! As always, if you have questions\/feedback, feel free to comment below or reach out to me on <a href=\"https:\/\/twitter.com\/tejaykodali\" target=\"_blank\">Twitter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this post, I will show you how you can use rbokeh to build interactive graphs and maps in R. What is bokeh? Bokeh is a popular python library used for building interactive plots and maps, and now it is also available in R, thanks to Ryan Hafen. It is a very powerful [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":3274,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[10,98,232],"class_list":["post-3214","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visualizing-data","tag-best-r-packages","tag-maps","tag-rstats"],"views":13698,"_links":{"self":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/3214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/comments?post=3214"}],"version-history":[{"count":0,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/3214\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media\/3274"}],"wp:attachment":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media?parent=3214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/categories?post=3214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/tags?post=3214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}