【问题标题】:Using rCharts to create Histogram使用 rCharts 创建直方图
【发布时间】:2015-08-06 10:09:01
【问题描述】:

我尝试使用来自 rCharts 的 nPlot 创建直方图,但未成功。我的数据格式如下:

> head(Data)
   X       Date Age Race Sex         region
 1 1 2013-12-31  54 <NA>   M     washington
 2 2 2013-12-31  20 <NA>   M     california
 3 3 2013-12-31  34    B   M north carolina
 4 4 2013-12-30  43    B   M   pennsylvania
 5 5 2013-12-30  31    W   F     california
 6 6 2013-12-29  43    W   M       colorado

使用 ggplot2(和 doBy 作为 summaryBy):

cdat <- summaryBy(Age~Sex,data=Data,FUN=c(mean),na.rm=TRUE)
cdat <- cdat[-3,]
ggplot(Data,aes(x=Age,fill=Sex))+
geom_histogram(binwidth=.7, alpha=.8, position="identity")+
geom_vline(data=cdat, aes(xintercept=Age.mean,  colour=Sex),
           linetype="dashed", size=1)

输出是一个由Sex分组的直方图:

我想使用 rCharts 包创建一个类似的图形。我尝试了以下但没有成功:

output$Histogram1 <- renderChart({
    Trial <- summaryBy(.~Age+Sex,data=Data,FUN=length)
    Histogram1 <- nPlot(x='Age',y='X.length',group='Sex',data=Trial,type='bar',dom='Histogram1')
    Histogram1$chart(margin = list(left = 100))
    return(Histogram1)
})

代码在 server.R 脚本中 -> 用于闪亮的应用程序。

【问题讨论】:

    标签: r histogram rcharts


    【解决方案1】:

    如果您的代码在闪亮的应用程序之外工作,请尝试使用 renderChart2 而不是 renderChart:

    output$Histogram1 <- renderChart2({
    Trial <- summaryBy(.~Age+Sex,data=Data,FUN=length)
    Histogram1 <- nPlot(x='Age',y='X.length',group='Sex',data=Trial,type='bar',dom='Histogram1')
    Histogram1$chart(margin = list(left = 100))
    return(Histogram1)
    })
    

    【讨论】:

      【解决方案2】:

      你能说出你所面临的确切错误吗?

      另外,我在nPlot 函数中使用type="bar" 时也遇到了一些问题。

      修复的方法是使用type="multiBarChart"

      希望有帮助!

      编辑 1:

      另外,正如@susacb 所建议的,使用renderChart2 代替renderChart 并删除dom 参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        • 2014-02-16
        • 2020-08-11
        • 2014-02-15
        • 2011-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多