【问题标题】:Rcharts nPlot() Percentages with discrete/multiBarChartRcharts nPlot() 离散/multiBarChart 的百分比
【发布时间】:2013-10-31 20:48:43
【问题描述】:

我想使用RCharts 中的nPlot() 函数来绘制属于离散组的人的百分比,而不是频率。

例如:使用下面的 HairEyeColor 数据/代码,我可以考虑不同头发颜色的人的百分比(我的分组变量),作为他们眼睛颜色的函数。

## server.r
library(rCharts)
library(shiny)
library(reshape)

HairEyeColor1 <- melt(round(prop.table(HairEyeColor[,,1],2)*100,2))
names(HairEyeColor1) <- c("Hair", "Eye", "Percent")

shinyServer(function(input, output) {
output$myChart <- renderChart({
p1 <- nPlot(Percent ~ Eye, group = "Hair", data = HairEyeColor1, type = multiBarChart")
p1$addParams(dom = 'myChart')
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")
)
)
))

假设我只想查看一个因素,例如头发。有没有办法使用nPlot() 绘制他们的百分比?

prop.table(rowSums(HairEyeColor[,,1]))
    Black     Brown       Red       Blond 
    0.2007168 0.5125448   0.1218638 0.1648746 

type=multiBarChart 我试过了:

p1 <- nPlot(Percent ~ , group = "Hair", data = HairEyeColor1, type = multiBarChart")

这完全失败了。我也试过:

p1 <- nPlot(Percent ~ 1, group = "Hair", data = HairEyeColor1, type = multiBarChart")

这至少将一些图表传递给 Shiny UI。但这看起来很可怕,功能(X/Y 轴的外观,点击图表)都丢失了。

我认为这适用于nPlot(type=discreteBarChart),但它的数据布局似乎需要一个具有单因素变量的数据框。所以我不太明白如何欺骗nPlot(type=discreteBarChart) 获取比例/百分比向量。

任何建议表示赞赏。

【问题讨论】:

  • 你能展示你想要制作的情节吗,使用基础 R、lattice 或 ggplot2 制作?这将帮助我告诉你如何使用 nPlot 制作它。
  • 希望得到类似:barplot(prop.table(rowSums(HairEyeColor[,,1])), ylim=c(0,1))
  • 我想我希望它堆叠(成单列)并且高度也达到 100%。并且可以通过单击右上角的因子级别来选择删除某些级别。但是当我更多地研究discreteBarChart 时,我意识到这不是一个选择。并且仅在multiBarChart 中可用。我想也是这样(显然添加了图例):barplot(as.matrix(prop.table(rowSums(HairEyeColor[,,1]))), ylim=c(0,1),beside=FALSE)

标签: r rcharts


【解决方案1】:

这里是使用nPlot 的方法。您可以看到结果图here。如果要默认堆叠条形图,请在打印图表之前添加行 n1$chart(stacked = TRUE)

# prepare data
require(plyr)
dat = as.data.frame(HairEyeColor)
dat = ddply(dat, .(Hair), summarize, Freq = sum(Freq), Group = "A")

# draw chart
require(rCharts)
n1 <- nPlot(Freq ~ Group, data = dat, group = 'Hair', type = 'multiBarChart')
n1

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2016-11-28
  • 1970-01-01
相关资源
最近更新 更多