Home > R > useR2011 Easy interactive ggplots talk

useR2011 Easy interactive ggplots talk

I’m talking tomorrow at useR! on making ggplots interactive with the gWidgets GUI framework. For those of you at useR, here is the code and data, so you can play along on your laptops. For everyone else, I’ll make the slides available in the next few days so you can see what you missed. Note that for confidentiality reasons, I’ve added some random junk to the sample values, so please don’t use this data for actual research. (If you are interested in chemical exposure data, contact the lab.)

Chromium data. Nickel data. Code for examples 1 to 4. Once we introduce example 5, the rest of the code needs a little reworking.

Update: The permissions on the code files were blocking downloads from people who aren’t me. My bad. It should be fixed now.

Another update: By popular demand, here are the code chunks.

library(ggplot2)
library(gWidgetstcltk)

chromium <- read.csv("chromium.csv")
nickel <- read.csv("nickel.csv")

p <- ggplot(chromium, aes(air, bm)) +
  geom_point()

win_ctrls <- gwindow("Plot controls 1-4")
grp_ctrls <- ggroup(container = win_ctrls, horizontal = FALSE)

#1 Changing scales
available_scales <- c(
  Linear      = "identity", 
  Log           = "log10"
)   
  
frm_scale_trans_y <- gframe(
  "Y scale transformation", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_scale_trans_y <- gradio(
  names(available_scales), 
  container = frm_scale_trans_y,
  handler = function(h, ...)
  {
    scale_trans_y <- available_scales[svalue(h$obj)]  
    p <<- p + 
      scale_y_continuous(
        trans = scale_trans_y
      )   
    print(p)  
  }
)

frm_scale_trans_x <- gframe(
  "X scale transformation", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_scale_trans_x <- gradio(
  names(available_scales), 
  container = frm_scale_trans_x,
  handler = function(h, ...)
  {
    scale_trans_x <- available_scales[svalue(h$obj)]  
    p <<- p + 
      scale_x_continuous(
        trans = scale_trans_x
      )   
    print(p)  
  }
)

#2 Changing facets
facet_choices <- list(
  None = ". ~ .",
  "RPE" = ". ~ rpe",
  "Welding type" = ". ~ welding.type",
  "RPE and Welding type" = "rpe ~ welding.type"
)

frm_facets <- gframe(
  "Faceting", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_facets <- gradio(
  names(facet_choices), 
  container = frm_facets,
  handler = function(h, ...)
  {
    facet_formula <- 
      facet_choices[[svalue(h$obj)]]
    p <<- p + 
      facet_grid(facet_formula)
    print(p) 
  }
)

#3 Changing the dataset                     
frm_datasets <- gframe(
  "Datasets", 
  container = grp_ctrls,
  expand = TRUE
)  

cmb_datasets <- gcombobox(
  c("chromium", "nickel"), 
  container = frm_datasets,
  handler = function(h, ...)
  {
    p <<- p %+% get(svalue(h$obj)) 
    print(p)  
  }
)

#4 Changing the title
frm_title <- gframe(
  "Plot title", 
  container = grp_ctrls,
  expand = TRUE
)  

txt_title <- gedit(
  p$options$title,
  container = frm_title,
  handler = function(h, ...)
  {  
    p <<- p + opts(title = svalue(txt_title))
    print(p)
  }
)   
library(ggplot2)
library(gWidgetstcltk)

chromium <- read.csv("chromium.csv")
nickel <- read.csv("nickel.csv")

p <- ggplot(chromium, aes(air, bm)) +
  geom_blank()
  
print_p <- function()
{
  pp <- get("p", envir = globalenv())
  if(svalue(chk_points)) pp <- pp + geom_point()  
  if(svalue(chk_smooth)) pp <- pp + geom_smooth()
  print(pp)
}

win_ctrls <- gwindow("Plot controls 1-5")
grp_ctrls <- ggroup(container = win_ctrls, horizontal = FALSE)

#1 Changing scales
available_scales <- c(
  Linear      = "identity", 
  Log           = "log10"
)   
  
frm_scale_trans_y <- gframe(
  "Y scale transformation", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_scale_trans_y <- gradio(
  names(available_scales), 
  container = frm_scale_trans_y,
  handler = function(h, ...)
  {
    scale_trans_y <- available_scales[svalue(h$obj)]  
    p <<- p + 
      scale_y_continuous(
        trans = scale_trans_y
      )   
    print_p()  
  }
)

frm_scale_trans_x <- gframe(
  "X scale transformation", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_scale_trans_x <- gradio(
  names(available_scales), 
  container = frm_scale_trans_x,
  handler = function(h, ...)
  {
    scale_trans_x <- available_scales[svalue(h$obj)]  
    p <<- p + 
      scale_x_continuous(
        trans = scale_trans_x
      )   
    print_p()  
  }
)

#2 Changing facets
facet_choices <- list(
  None = ". ~ .",
  "RPE" = ". ~ rpe",
  "Welding type" = ". ~ welding.type",
  "RPE and Welding type" = "rpe ~ welding.type"
)

frm_facets <- gframe(
  "Faceting", 
  container = grp_ctrls,
  expand = TRUE
)  

rad_facets <- gradio(
  names(facet_choices), 
  container = frm_facets,
  handler = function(h, ...)
  {
    facet_formula <- 
      facet_choices[[svalue(h$obj)]]
    p <<- p + 
      facet_grid(facet_formula)
    print_p() 
  }
)

#3 Changing the dataset                     
frm_datasets <- gframe(
  "Datasets", 
  container = grp_ctrls,
  expand = TRUE
)  

cmb_datasets <- gcombobox(
  c("chromium", "nickel"), 
  container = frm_datasets,
  handler = function(h, ...)
  {
    p <<- p %+% get(svalue(h$obj)) 
    print_p()  
  }
)

#4 Changing the title
frm_title <- gframe(
  "Plot title", 
  container = grp_ctrls,
  expand = TRUE
)  

txt_title <- gedit(
  p$options$title,
  container = frm_title,
  handler = function(h, ...)
  {  
    p <<- p + opts(title = svalue(txt_title))
    print_p()
  }
)

#5 Changing geoms
frm_geoms <- gframe(
  "Add/remove geoms", 
  container = grp_ctrls,
  expand = TRUE
)  

chk_points <- gcheckbox(
  "Points", 
  container = frm_geoms,
  handler = function(h, ...) print_p()  
)      
chk_smooth <- gcheckbox(
  "Smooth curve", 
  container = frm_geoms,
  handler = function(h, ...) print_p()  
)     
About these ads
  1. 18th August, 2011 at 0:23 am

    Good luck Richard, go you!

  2. 18th August, 2011 at 12:00 pm

    Code link is broken for me…?

    • 18th August, 2011 at 22:47 pm

      Sorry, the permissions on the doc weren’t public enough. Should be working now.

  3. 18th August, 2011 at 13:34 pm

    Excellent talk; it did look easy (and useful).

  4. 18th August, 2011 at 14:02 pm

    Google says the code example pages are not available.

  5. chrisbeeleyimh
    18th August, 2011 at 17:01 pm

    Great talk, thanks very much. I’m having difficulty downloading the code though, are the links okay?

  1. 20th August, 2011 at 8:05 am
  2. 20th August, 2011 at 23:26 pm
  3. 26th August, 2011 at 15:07 pm
  4. 1st September, 2011 at 23:06 pm

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

Follow

Get every new post delivered to your Inbox.

Join 204 other followers

%d bloggers like this: