r语言 - RCharts 折线图功能



我正在尝试将 RChart 嵌入到一个闪亮的应用程序中。 具体来说,使用函数nPlottype=lineChart NVD3 功能。我正在尝试为模拟的正常数据绘制 3 条密度曲线。我正在尝试实现以下示例中描述的一些功能:

http://nvd3.org/ghpages/line.html

我的问题:

  1. 如何为不同的密度曲线提供不同的颜色?
  2. 如何具有单击组变量(1),(2),(3)的功能以有效地删除所选密度。类似于单击示例中的"正弦波"(右上角)以删除橙色正弦曲线?
  3. 如何添加 x 轴和 y 轴标签?我的$params$xAxis=呼叫不起作用。

下面是我的服务器。R 和 ui。R 文件:

## server.r
library(rCharts)
library(shiny)
x <-rnorm(1000,0,1)
y <-rnorm(1000,1,1)
z <-rnorm(1000,2,1)
out <- c(x,y,z)
grp <- c(rep(0,1000),rep(1,1000),rep(2,1000))
data <- as.data.frame(cbind(out,grp))
dens <- by(data$out, data$grp, density)
d <- unlist(c(dens[[1]][1][1], dens[[2]][1][1], dens[[3]][1][1]))
support <- unlist(c(dens[[1]][2][1], dens[[2]][2][1], dens[[3]][2][1]))
grpvar <- c(rep(0,length(unlist(dens[[1]][1][1]))), rep(1,length(unlist(dens[[2]][1][1]))), rep(2,length(unlist(dens[[3]][1][1]))))
dat <- as.data.frame(cbind(d,support,grpvar))

shinyServer(function(input, output) {
output$myChart <- renderChart({
p1 <- nPlot(support~d, group=grpvar, data = dat, type = "lineChart")
p1$addParams(dom = 'myChart')
p1$params$width = 600
p1$params$height = 400
p1$params$xAxis = "Support"
p1$params$yAxis = "Density"
p1$chart(tooltipContent = "#! function(key, x, y, e){
return '<b>Group</b>: ' + e.point.grpvar
} !#")
return(p1)
})
})

## ui.R
library(rCharts)
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("rCharts: Interactive Charts from R using NVD3.js"),
sidebarPanel(
wellPanel(
    helpText(   "Look at the pretty graph"
    )
    ),
wellPanel(
    helpText(   "Look at the pretty graph"
    )
    ),
wellPanel(
    helpText(   "Look at the pretty graph"
    )
    )
),

mainPanel(
div(class='wrapper',
tags$style(".Nvd3{ height: 600px;}"),
showOutput("myChart","Nvd3")
)
)
))

提前感谢您提供的任何帮助/建议。

你只需要在grpvar周围加上引号,所以

p1 <- nPlot(support~d, group="grpvar", data = dat, type = "lineChart")

最新更新