我正在尝试将 RChart 嵌入到 Shiny 的应用程序中。我专门使用 nPlot
函数来创建 type=scatterChart
NVD3 样式图。在下面的 NVD3 网站示例中,我有兴趣在我的 RCharts Shiny 应用程序中使用两个功能:
http://nvd3.org/ghpages/scatter.html
- 似乎在上述示例的 x 轴和 y 轴上包含了一个“地毯”,略微展示了 x 点和 y 点在其各自支撑点上最常出现的位置。
- 此外,当点击图表并将鼠标悬停在特定点上时,会出现一条垂直线和水平线,标明相应点的 (x,y) 位置。
有谁知道如何扩展我下面的代码来实现这两个功能。下面包含 Shiny 的 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")
)
)
))
提前感谢您提供的任何建议。
最佳答案
这可以使用以下代码实现:
p1$chart(
showDistX = TRUE,
showDistY = TRUE
)
return(p1)
此外,请注意,虽然直接操作 p1$params
有效,但在中指定 height
和 width
可能更安全这样:
p1 <- nPlot(
y ~ x,
data = dat,
type = "scatterChart",
height = 400,
width = 650
)
关于RCharts 散点图增强功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085351/