gpt4 book ai didi

rCharts Polychart : Adding horizontal or vertical lines to a plot

转载 作者:行者123 更新时间:2023-12-04 12:13:53 25 4
gpt4 key购买 nike

我在理解如何使用 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 rPlot R 中的函数。

我做了一些挖掘,我觉得答案与添加/修改 layers 有关。 parameterrPlot目的。
#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 图上。将任何其他图层的值作为列添加到原始 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 .

关于rCharts Polychart : Adding horizontal or vertical lines to a plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com