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()
)
Leave a Reply Cancel reply
Categories
Archives
- April 2013
- December 2012
- November 2012
- October 2012
- July 2012
- June 2012
- May 2012
- March 2012
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
Blogroll
Licensing

The non-code parts of 4D Pie Charts by Richard Cotton are licensed under a Creative Commons Attribution-NoDerivs 2.0 UK: England & Wales License. The code parts of the blog are licensed under the WTFPL v2.0.
Good luck Richard, go you!
Code link is broken for me…?
Sorry, the permissions on the doc weren’t public enough. Should be working now.
Excellent talk; it did look easy (and useful).
Google says the code example pages are not available.
Great talk, thanks very much. I’m having difficulty downloading the code though, are the links okay?