Google Public Data Explorer - you can explore and manipulate a variety of datasets that are available publically from all over the world
World Development Indicators - our data is a subset of data taken from this larger databank that is available as a free download
It has the following functionality: - slider: will add a sliding scale of numerical values - checkbox: will add a check box - picker: will add a drop down menu
The above widgets and your plot are defined within the function manipulate(). Each component is separated using a comma.
Important: if using ggplot, you must define your plot first, and then define your manipulations.
# Install and load the manipulate package
# install.packages("manipulate")
library(manipulate)
# Load other required packages
library(ggplot2)
library(reshape2) # For data frame reshaping
# Read in Fertility/Life Expectancy data
# Column names cannot start with a number so R added an X in front each year
d<-read.csv("C:/Users/Christie/Documents/PSYCH 670/Interactive Plots/wdi.csv")
# Change from short to long format (melt is from the reshape2 package)
# Will create new columns:
# variable - each year, which was now a column, is now in one column called variable
# value - column of values corresponding to each year in variable
d<- melt(d, id.vars = c("CntryName", "Region","CntryCode","Ind"))
# Subset for each indicator
dfert<-subset(d,d$Ind=="Fertility") # Subset of Fertility data
dexp<-subset(d,d$Ind=="Expect") # Subset of Life Expectancy data
dpop<-subset(d,d$Ind=="Population") # Subset of Population data
# Adds columns of Life Expectancy and Population values to the Fertility data subset
# to reate one large data frame with all of the data
d<-data.frame(dfert,dexp$value,dpop$value)
# Name data frame columns
names(d)<-c("CntryName","Region","CntryCode","Ind","Year","Fertility","Expectancy","Population")
# Remove the X before the year and change the data type to numeric
# This is necessary so that we can use slider to change the year
# Slider only works with numeric values
d$Year<-as.numeric(substring(d$Year,2))
# Returns the data frame for plotting depending on selected options
# yr (integer): year;
# cntry (character): country;
# all.yrs (logical): true is all years
fn.plot2<-function(yr,cntry,all.yrs) {
# Subset by selected year if all years is not selected
if (all.yrs==FALSE) {
dp<-subset(d,d$Year==yr)
} else {
dp<-d
}
# Subset by country if all is not selected
if (cntry != "All") {
dp<-subset(dp,dp$CntryName==cntry)
}
return(dp)
}
# Build plot and add slider for year, drop-down menu for country, and check box for all years
# manipulate(
# ggplot(fn.plot2(Year,Country,All.Years))+
# geom_point(aes(x=Expectancy,y=Fertility,size=Population,colour=Region),alpha=0.8)+
# scale_x_continuous(limits=c(20,90))+
# scale_y_continuous(limits=c(0,10))+
# scale_size_area(max_size=10)+
# theme_bw(),
# Year=slider(1960,2011),
# Country = picker(as.list(c("All",unique(as.character(d$CntryName))))),
# All.Years = checkbox(TRUE, "All Years")
#)
Manipulate is nice because it is fairly quick and easy to use.
The draw backs of manipulate are that: - it will only work in Rstudio - you only have access to the manipulate GUI from inside the plotting pane - it has a limited set of functionality: slider, drop down and checkbox - if you open a new window for the plot with the zoom button, you will not be able to manipulate the plot in that window.
A more versatile method of manipulating a plot is by creating a GUI that is independent from the plotting pane. This can be done using: - gWidgetstcltk: toolbox that creates interactive GUIs
Pacakge documentation - gWidgetstcltk: toolbox - gWidgets: widgets that can be placed in the GUI, such as check boxes
# Install and load required package
# install.packages(gWidgetstcltk)
library(gWidgetstcltk)
# Create the handler (function that the GUI will run when options are selected)
p <- function(...) {
# svalue will grab the value the variable has been set too
# Subsets based on year if all years has not been selected
if (svalue(all.yrs)=="FALSE") {
df<-subset(d2,d2$Year==svalue(yr))
} else {
df<-d2
}
# Subset by country if all is not selected
if (svalue(cntry) != "All") {
df<-subset(df,df$CntryName==svalue(cntry))
}
# Create the plot
plot<-ggplot(df,aes(x=Expectancy,y=Fertility))+
geom_blank()+
scale_x_continuous(limits=c(20,90))+
scale_y_continuous(limits=c(0,10))+
scale_size_area(max_size=10)+
theme_bw()
# Add the geom based on selection
if (svalue(type)=="Points") {
plot<-plot+geom_point(aes(size=Population,colour=Region),alpha=0.8)
} else if (svalue(type)=="Smooth") {
plot<-plot+geom_smooth(aes(colour=Region),method="auto",alpha=0.8,size=2)
}
print(plot)
}
# Create window and add widgets
# win_ctrls <- gwindow("Plot controls")
# tbl = glayout(container=win_ctrls)
# tbl[1,1]<-"Year: "
# tbl[1,2] <- (yr <- gspinbutton(from = 1960, to= 2011, by=1, value = 1960,container = tbl,handler=p))
# tbl[2,1]<-"All Years? "
# tbl[2,2]<- (all.yrs<-gcheckbox("Yes", container=tbl,handler=p))
# tbl[3,1]<-"Country: "
# tbl[3,2]<-(cntry<-gcombobox(c("All",unique(as.character(d2$CntryName))), editable=FALSE, container=tbl, handler=p))
# tbl[4,1]<-"Plot Type: "
# tbl[4,2,expand=TRUE]<-(type<-gradio(c("Points","Smooth"),selected=1,index=FALSE,horizontal=TRUE,container=tbl,handler=p))
This is far more versatile, but what if we wanted to share our interactive plot on the internet?
The googleVis package can do just that.
This is also a good reference for using googleVis.
Depending on your security settings, you might not be able to view the flash graph in your web browser from a file opened locally. Once the file is published to Rpubs (or anywhere else online), you will be able see it and interact with it.
p<-gvisMotionChart(d, idvar = "CntryName", timevar = "Year",xvar="Expectancy", yvar="Fertility", colorvar="Region", sizevar="Population", date.format = "%Y/%m/%d")
print(p,'chart')