簡體   English   中英

rCharts Polychart:向繪圖添加水平或垂直線條

[英]rCharts Polychart: Adding horizontal or vertical lines to a plot

我在使用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有一些相似之處,我也沒有多少經驗。

謝謝你的建議!

您可以使用圖層將其他圖形疊加到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網站 ,另一個顯示如何覆蓋回歸線

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM