【发布时间】: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 脚本中 -> 用于闪亮的应用程序。
【问题讨论】: