Pages

Tuesday, October 22, 2013

Dynamic plots with R Studio.


Few days back i came to know that R Studio provides dynamic plots like you can plot histograms and move sliders, tick check boxes and also you can select from the drop down list the items you want to display from your dataset.I will provide some examples of this below. Quite cool enough from Rstudio group.

//Sliders
library(mosaic)
if(require(manipulate)) {
manipulate(
histogram( ~ eruptions, data=faithful, n=n),
n = slider(5,40)
)

}

//CheckBoxes
library(mosaic)
if(require(manipulate)) {
manipulate(
histogram( ~ age, data=HELP, n=n, density=density),
n = slider(5,40),
density = checkbox()
)
}

Check box with density plot



//Dropdowns

library(mosaic)
if(require(manipulate)) {
manipulate(
histogram( ~ age, data=HELP, n=n, fit=distribution, dlwd=4),
n = slider(5,40),
distribution =
picker('normal', 'gamma', 'exponential', 'lognormal',
label="distribution")
)
}


//dropdown and density

library(mosaic)
manipulate(
  histogram(as.matrix(mtcars[,factor]), 
          beside = TRUE, main = factor,density=density),
  factor = picker("mpg", "disp", "hp","drat","wt"),density = checkbox())



This way you can make your ggplots dynamic with the manipulate package.




Post a Comment