4

我在理解如何使用包rPlot中的函数自定义图表时遇到了一些麻烦rCharts。说我有以下代码

#Install rCharts if you do not already have it
#This will require devtools, which can be downloaded from CRAN
require(devtools)
install_github('rCharts', 'ramnathv')

#simulate some random normal data
x <- rnorm(100, 50, 5)
y <- rnorm(100, 30, 2)

#store in a data frame for easy retrieval
demoData <- data.frame(x,y)

#generate the rPlot Object
demoChart <- rPlot(y~x, data = demoData, type = 'point')

#return the object // view the plot
demoChart

这将生成一个图,这很好,但是我将如何沿 y 轴添加水平线?例如,如果我想画一条代表平均 y 值的绿线,然后画一条代表平均值 +/- 3 个标准偏差的红线?如果有人知道一些文档并可以指出我,那就太好了。但是,我能找到的唯一文档是 polychart.js ( https://github.com/Polychart/polychart2 ),我不太确定如何将其应用于rCharts rPlotR 中的函数。

我已经做了一些挖掘,我觉得答案将与添加/修改对象layers parameter内部有关rPlot

#look at the slots in this object
demoChart$params$layers

#doing this will return the following output (which will be different for
#everybody because I didn't set a seed). Also, I removed rows 6:100 of the data.

demoChart$params$layers
[[1]]
[[1]]$x
[1] "x"

[[1]]$y
[1] "y"

[[1]]$data
       x        y
1   49.66518 32.75435
2   42.59585 30.54304
3   53.40338 31.71185
4   58.01907 28.98096
5   55.67123 29.15870


[[1]]$facet
NULL

[[1]]$type
[1] "point"

如果我解决了这个问题,我会发布一个解决方案,但在此期间我将不胜感激任何帮助/建议!我没有太多在 R 中玩对象的经验。我觉得这应该有一些相似之处ggplot2,我也没有太多经验。

感谢您的任何建议!

4

1 回答 1

3

您可以使用图层将其他图形叠加到您的 rCharts 图上。将任何其他图层的值作为列添加到原始 data.frame。copy_layer允许您在额外层中使用来自 data.frame 的值。

# Regression Plots using rCharts
require(rCharts)

mtcars$avg <- mean(mtcars$mpg)
mtcars$sdplus <- mtcars$avg + sd(mtcars$mpg)
mtcars$sdneg <-  mtcars$avg - sd(mtcars$mpg)

p1 <- rPlot(mpg~wt, data=mtcars, type='point')

p1$layer(y='avg', copy_layer=T, type='line', color=list(const='red'))
p1$layer(y='sdplus', copy_layer=T, type='line', color=list(const='green'))
p1$layer(y='sdneg', copy_layer=T, type='line', color=list(const='green'))

p1 

这里有几个例子:一个来自主要的rCharts 网站,另一个展示了如何覆盖回归线

于 2014-04-12T12:49:18.643 回答