Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:11
Show Gist options
  • Save timelyportfolio/0de0d6cea4cff09bc331 to your computer and use it in GitHub Desktop.
Save timelyportfolio/0de0d6cea4cff09bc331 to your computer and use it in GitHub Desktop.
density plot with rCharts
library(ggplot2)
library(pipeR)
library(rCharts)
#using ggplot2 stat_density help example
library(plyr) # to access round_any
movies$decade <- round_any(movies$year, 10)
# this is too slow but might work with your data
ggplot_build(
ggplot(
movies
, aes(x=rating, colour=decade, group=decade)
) + geom_density(fill=NA)
) %>>%
(nPlot(
data = .$data[[1]][,c("x","y","group")]
,x = "x"
,y = "y"
,group = "group"
,type = "lineChart"
))
gB <- ggplot_build(
ggplot(
movies
, aes(x=rating, colour=decade, group=decade)
) + geom_density(fill=NA)
)
gB$data[[1]]$group = levels(factor(gB$plot$data[,gB$plot$labels$group]))[gB$data[[1]]$group]
gB %>>%
(dPlot(
data = .$data[[1]][,c("x","y","group")]
,x = "x"
,y = "y"
,groups = "group"
,type = "line"
,defaultColors = unique(.$data[[1]]$colour)
,yAxis = list( outputFormat = '.0%' )
)) -> d1
d1$setTemplate(afterScript = "
<script>
{{chartId}}[0].axes[0].shapes.call(
d3.svg.axis()
.scale(
d3.scale.linear()
.domain(d3.extent({{chartId}}[0].axes[0]._scale.domain(),function(d){return parseInt(d)}))
.range(d3.extent({{chartId}}[0].axes[0]._scale.range(),function(d){return parseInt(d)}))
)
.orient('bottom')
).selectAll('.tick > text')
.attr('transform','')
</script>
")
d1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment