2015-04-17 52 views
6

有沒有像在dygraphs網站上一樣在R中使用不同類型的圖與dygraphs http://dygraphs.com/tests/plotters.html?使用R時有沒有辦法訪問這些?一個從R網站的dygraphs中取得的簡單例子如下:在R中使用dygraphs包創建一個barplot

library(dygraphs) 
library(dplyr) 
lungDeaths <- cbind(mdeaths, fdeaths) 
dygraph(lungDeaths) 
# How to choose a different type of plot? 

/編輯。所以我發現你可以在這裏使用dygraph的自定義繪圖儀:http://blog.dygraphs.com/2012/08/introducing-custom-plotters.html。有什麼方法可以在R中獲得?

+0

嘗試'ggplot2'包。 –

+0

我知道ggplot2,但我想在HTML文件中以交互方式使用它。同步功能對我來說非常有吸引力!我想有一個由這樣的dygraphs提供的範圍選擇器:http://rstudio.github.io/dygraphs/gallery-range-selector.html – pfuhlert

回答

6

我在dyOptions中使用繪圖儀來創建條形圖。我剛剛從dygraphs博客複製了barChartPlotter函數。 http://blog.dygraphs.com/2012/08/introducing-custom-plotters.html

library(xts) 
library(dygraphs) 
library(lubridate) 

graph_data <- xts(x = c(1,2,3,4), order.by = lubridate::ymd(c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01"))) 
names(graph_data) <- "value" 

dygraph(graph_data) %>% 
    dyOptions(useDataTimezone = TRUE, plotter = 
       "function barChartPlotter(e) { 
       var ctx = e.drawingContext; 
       var points = e.points; 
       var y_bottom = e.dygraph.toDomYCoord(0); // see  http://dygraphs.com/jsdoc/symbols/Dygraph.html#toDomYCoord 

       // This should really be based on the minimum gap 
       var bar_width = 2/3 * (points[1].canvasx - points[0].canvasx); 
       ctx.fillStyle = e.color; 

       // Do the actual plotting. 
       for (var i = 0; i < points.length; i++) { 
        var p = points[i]; 
        var center_x = p.canvasx; // center of the bar 

        ctx.fillRect(center_x - bar_width/2, p.canvasy, 
           bar_width, y_bottom - p.canvasy); 
        ctx.strokeRect(center_x - bar_width/2, p.canvasy, 
           bar_width, y_bottom - p.canvasy); 
       } 
       }") 
+0

哇,真好!謝謝。我用NVD3獲得了股票,但這是我的問題的完美答案。 – pfuhlert

+0

上週一我有同樣的問題,並認爲別人可能會覺得它有用。 NVD3非常好! – KERO

2

嘗試rChartsNVD3。這是不可能放大圖表,但海事組織是更花哨,一些選擇功能很酷。

+0

好吧,我結束了使用NVD3,所以感謝您的答案。我會選擇它作爲解決方案。 – pfuhlert