RCharts scatterChart增强功能

时间:2013-09-30 01:51:09

标签: r nvd3.js rcharts

我正在尝试将RChart嵌入到闪亮的应用中。我特意使用nPlot函数创建type=scatterChart NVD3样式图。在下面的NVD3网站示例中,我有兴趣在我的RCharts闪亮应用中使用两项功能:

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

  1. 看起来沿着上述例子的x轴和y轴包括“地毯”,显示x和y点沿着它们各自的支撑最频繁出现的位置。
  2. 此外,当单击图表并将鼠标悬停在特定点上时,会出现垂直和水平线,注明相应点的(x,y)位置。
  3. 有谁知道如何扩展我的代码以实现这两项功能。下面包含闪亮的server.r和ui.r脚本。

    ## server.r
    library(rCharts)
    library(shiny)
    
    x <- rnorm(100)
    y <- rnorm(100)
    dat <- as.data.frame(cbind(x,y))
    
    shinyServer(function(input, output) {
    
    output$myChart <- renderChart({
    
    p1 <- nPlot(y ~ x, data = dat, type = "scatterChart")
    
    p1$addParams(dom = 'myChart')
    
    p1$params$height=400
    p1$params$width=650
    
    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: 400px;}"),
    showOutput("myChart","Nvd3")
    )
    
    )
    ))
    

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

1 个答案:

答案 0 :(得分:6)

这可以使用以下代码实现:

p1$chart(
  showDistX = TRUE,
  showDistY = TRUE
)

return(p1)

另外,正如注释一样,虽然p1$params的直接操作有效,但以这种方式指定heightwidth可能更安全:

p1 <- nPlot(
  y ~ x,
  data = dat,
  type = "scatterChart",
  height = 400,
  width = 650
)
相关问题