我在理解如何使用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标准差的红线?如果有人知道一些文档,并能给我指点,那就太好了。然而,我能找到的唯一文档是关于polychart.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有一些相似之处。
谢谢你的建议!
发布于 2014-04-12 20:49:19
您可以使用图层将其他图形叠加到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 website,另一个展示如何overlay a regression line。
https://stackoverflow.com/questions/20107947
复制相似问题