楼主: oliyiyi
2086 0

Interactive plotting with rbokeh [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
66105 个
通用积分
31671.0967
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2016-2-18 15:17:19 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

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 for creating good looking plots for the web easily, and it is fully compatible with shiny.

Generally, plotting in bokeh is done by adding layers to a plot, similar to ggplot2. For creating a simple plot, there are two main steps involved:

  • figure() – This will initialize the bokeh plot. It has a variety of parameters to set width, height, title, and axes parameters.
  • ly_geom() – This will specify the type of geom you want to use. There are a variety of options, including ly_points, ly_lines, ly_hist, ly_boxplot, etc. Each of these have parameters which allow for specifying size, color, what to show on hover, etc.

Okay, let’s start building some visualizations! Installation instructions are available here.

In one of previous posts, I showed how you can do Hiearchical Clustering in R, and demonstrated it with the iris dataset. Let’s recreate the visualization using rbokeh:

  1. clusters <- hclust(dist(iris[, 3:4]), method = 'average')
  2. clusterCut <- cutree(clusters, 3)
  3. p <- figure(title = 'Hierarchical Clustering of Iris Data') %>%  
  4.   ly_points(Petal.Length, Petal.Width, data = iris, color = Species, hover = c(Sepal.Length, Sepal.Width)) %>%
  5.   ly_points(iris$Petal.Length, iris$Petal.Width, glyph = clusterCut, size = 13)
  6. p
复制代码

which gives us the following plot:

[color=rgb(255, 255, 255) !important]


All the points where the two colors don’t match are the ones that were clustered in correctly.

Now, let’s build a chart to show apple stock data for the past year. The data was obtained from Yahoo Finance.

  1. aapl <- read.csv('aapl.csv')
  2. aapl$Date <- as.Date(aapl$Date)
  3. p <- figure(title = 'Apple Stock Data') %>%  
  4.   ly_points(Date, Volume / (10 ^ 6), data = aapl, hover = c(Date, High, Open, Close)) %>%
  5.   ly_abline(v = with(aapl, Date[which.max(Volume)])) %>%
  6.   y_axis(label = 'Volume in millions', number_formatter = 'numeral', format = '0.00')
复制代码

which gives us the following plot (with a vertical line on the date with the highest amount of volume):

[color=rgb(255, 255, 255) !important]


In another previous post, I demonstrated how you can use Leaflet to build Interactive Maps. Let’s recreate this using rbokeh:

  1. SFData <- read.csv('SFPD_Incidents_-_Previous_Year__2015_.csv')
  2. data <- subset(SFData, Category == 'BRIBERY' | Category == 'SUICIDE')
  3. p <- gmap(lat = 37.78, lng = -122.42, zoom = 13) %>%
  4.   ly_points(Y, X, data = data, hover = c(Category, PdDistrict), col = 'red') %>%
  5.   x_axis(visible = FALSE) %>%
  6.   y_axis(visible = FALSE)
复制代码

which gives us the following plot:

[color=rgb(255, 255, 255) !important]


We can also somewhat replicate the facet_grid feature from ggplot2 as follows. We will use thediamonds dataset from ggplot2.

  1. diamonds <- ggplot2:: diamonds
  2. l <- levels(diamonds$color)
  3. plot_list <- vector(mode = 'list', 7)

  4. for (i in 1:length(l)) {
  5.   data <- subset(diamonds, color == l[i])
  6.   plot_list[[i]] <- figure(width = 350, height = 350) %>%
  7.     ly_points(carat, price, data = data, legend = l[i], hover = c(cut, clarity))
  8. }

  9. grid_plot(plot_list, nrow = 2)
复制代码

which gives us this plot:

[color=rgb(255, 255, 255) !important]


Pretty cool, don’t you think? If you want to learn more: The official documentation. 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.

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 Twitter.











二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Interactive interact Active inter plot compatible available building creating everyone

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-2 06:52