我正在尝试使用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 (和summaryBy的doBy ):
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脚本->中。
发布于 2015-08-07 23:24:10
如果你的代码在闪亮的应用程序之外运行,请尝试使用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)
})发布于 2016-04-06 20:40:08
你能说出你所面临的确切错误吗?
此外,当我在nPlot函数中使用type="bar"时,我也遇到了一些问题。
修复它的方法是使用type="multiBarChart"
希望这能有所帮助!
编辑1:
此外,正如@susacb建议的那样,使用renderChart2而不是renderChart,并删除dom参数。
https://stackoverflow.com/questions/31852930
复制相似问题