如何用rCharts绘制密度?

时间:2014-06-07 16:51:02

标签: r rcharts

我刚刚发现了rCharts。有可能做密度图吗?例如,对于ggplot2我做:

library(ggplot2)
m <- ggplot(movies, aes(x = rating))
m + geom_density(aes(fill=factor(Drama)), size=2)

谢谢!

1 个答案:

答案 0 :(得分:1)

我能够做到这一点

library(rCharts)
drama = density(movies$rating[movies$Drama==1])
drama = data.frame(drama$x, drama$y)
drama$Var = "Drama"
names(drama) = c("x", "y", "Var")

Nodrama = density(movies$rating[movies$Drama==0])
Nodrama = data.frame(Nodrama$x, Nodrama$y)
Nodrama$Var = "No Drama"
names(Nodrama) = c("x", "y", "Var")
densen1n2 = rbind(drama, Nodrama)
nPlot(x = "x", y = "y", group = "Var", data = densen1n2, type = "lineChart")

不确定这是否是最佳方式。我还不确定如何给x轴命名