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()
)




