Interactive view in ggplot.

Somedays ago, I knew R package “shiny“.
This package is good tool to make interactive view in R.
There is a famous tool Spotfire. Use with ggplot, it will be cool.
To use shiny, I wrote two scripts, server.R and ui.R.
ui.R defines user interface, and server.R defines plot.
I wrote interactive view about iris.

#server.R
library(ggplot2)
library(shiny)

shinyServer(function(input, output, session) {
  dataset <- reactive({
    iris[,c(input$xcol, input$ycol)]
  })
  
  output$plot1 <- renderPlot({
    par(mar = c(5.1, 4.1, 0, 10))
    p <- ggplot(dataset(), aes_string(x=input$xcol, input$ycol))+geom_point(aes(colour=iris$Species))
    print(p)
  
  })
})

Next, I wrote ui.R

#ui.R
library(ggplot2)
library(shiny)

shinyUI(pageWithSidebar(
  headerPanel('IRIS'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris),
  selected=names(iris)[[2]])
  ),
  mainPanel(
    plotOutput("plot1")
    )
  ))

The code provide the interactive scatter plot for user.
OK, open two file from R studio, then Run app.
Local server was stared and, got following view.

Screen Shot 2014-09-18 at 11.16.09 PM

Cool ! 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s