使用 dygraphs 包在 R 中创建条形图
Create a barplot in R using dygraphs package
有没有办法像在 dygraphs 网站上那样在 R 中使用不同类型的 dygraphs 图 http://dygraphs.com/tests/plotters.html?使用 R 时有没有办法访问这些?从 R 网站的 dygraphs 中获取的一个简单示例如下所示:
library(dygraphs)
library(dplyr)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)
# How to choose a different type of plot?
/编辑。所以我发现您可以使用带有 dygraphs 的自定义绘图仪,如下所示:http://blog.dygraphs.com/2012/08/introducing-custom-plotters.html。有什么办法可以在 R 中得到它吗?
我使用 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);
}
}")
有没有办法像在 dygraphs 网站上那样在 R 中使用不同类型的 dygraphs 图 http://dygraphs.com/tests/plotters.html?使用 R 时有没有办法访问这些?从 R 网站的 dygraphs 中获取的一个简单示例如下所示:
library(dygraphs)
library(dplyr)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)
# How to choose a different type of plot?
/编辑。所以我发现您可以使用带有 dygraphs 的自定义绘图仪,如下所示:http://blog.dygraphs.com/2012/08/introducing-custom-plotters.html。有什么办法可以在 R 中得到它吗?
我使用 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);
}
}")