使用dygraphs包在R中创建条形图 [英] Create a barplot in R using dygraphs package
问题描述
是否有一种方法可以像在dygraphs网站本身上那样,在R中使用不同种类的图表,例如dygraphs网站本身 http://dygraphs.com/tests/plotters.html ?使用R时是否可以访问这些文件?从R网站的书形图中选取的一个简单示例如下所示:
Is there a way to use different kinds of plots with dygraphs in R like on the dygraphs website itself http://dygraphs.com/tests/plotters.html? Is there a way to access these when using R? A simple example taken from the dygraphs for R website would look like:
library(dygraphs)
library(dplyr)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)
# How to choose a different type of plot?
/编辑.因此,我发现您可以将自定义绘图仪与图谱一起使用,如下所示: http ://blog.dygraphs.com/2012/08/introducing-custom-plotters.html .有什么办法可以在R中得到它吗?
/edit. So I found out that you can use custom plotters with dygraphs like on here: http://blog.dygraphs.com/2012/08/introducing-custom-plotters.html. Is there any way to get that in R?
推荐答案
我在dyOptions中使用了绘图仪来创建条形图.我只是从dygraphs博客复制了barChartPlotter函数. http://blog.dygraphs.com/2012/08/introducing-custom -plotters.html
I used plotter in dyOptions to create a bar chart. I just copied the barChartPlotter function from the dygraphs blog. 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中创建条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!