我在理解如何使用rCharts
包中的rPlot
函数自定义图形时遇到了一些问题。假设我有以下代码
#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标准偏差的红色线?如果有人知道一些文件,可以给我指一下,那就太好了。然而,我能找到的唯一文档是polycart.js(https://github.com/Polychart/polychart2)我不太确定如何将其应用于R.中的rCharts
rPlot
函数
我已经做了一些挖掘,我觉得答案与在rPlot
对象中添加/修改layers
parameter
有关。
#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
有一些相似之处,我也没有太多的经验。
谢谢你的建议!
您可以使用图层将其他图形覆盖到rCharts图上。将任何附加层的值作为列添加到原始数据帧中。copy_layer
允许您在附加层中使用数据帧中的值。
# 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网站,另一个展示了如何覆盖回归线。