In the paper we described the animint2dir(viz)
function which compiles and renders and animint viz list. In the code chunks below we use structure(viz, class="animint")
in order to render the animint inside of this Rmd document (animint2dir
is used internally).
Each section below contains
library(animint)
data(tips, package = "reshape2")
bar <- ggplot(tips) +
geom_bar(aes(x = sex, clickSelects = sex))
scatter <- ggplot(tips) +
geom_point(aes(x = total_bill, y = tip), alpha = 0.3) +
geom_point(aes(x = total_bill, y = tip, showSelected = sex))
structure(list(bar = bar, scatter = scatter), class = "animint")
data(WorldBank)
WorldBank$region <-
sub(" (all income levels)", "", WorldBank$region, fixed=TRUE)
years <- data.frame(year=unique(WorldBank$year))
wb.viz <- list(
timeSeries=ggplot()+
geom_tallrect(aes(
xmin=year-0.5, xmax=year+0.5, clickSelects=year),
alpha=0.5,
data=years)+
guides(color="none")+
geom_line(aes(year, life.expectancy, group=country, colour=region,
showSelected=region,
clickSelects=country),
data=WorldBank, size=4, alpha=3/5),
scatterPlot=ggplot()+
geom_point(aes(fertility.rate, life.expectancy, clickSelects=country,
showSelected=year, colour=region, size=population,
tooltip=paste(country, "population", population),
key=country), # key aesthetic for animated transitions!
data=WorldBank)+
geom_text(aes(fertility.rate, life.expectancy, label=country,
clickSelects=country,
showSelected=country, showSelected2=year,
showSelected3=region,
key=country), #also use key here!
data=WorldBank)+
scale_size_animint(breaks=10^(9:5))+
geom_text(aes(
5, 80, label=paste("year =", year), key=year, showSelected=year),
data=years),
time=list(variable="year",ms=3000),
duration=list(year=1000),
selector.types=list(country="multiple", region="multiple"),
first=list(
year=1979,
country=c("United States", "Vietnam"),
region=c("East Asia & Pacific", "North America")
))
structure(wb.viz, class="animint")
library(maps)
library(plyr)
data(UStornadoes)
ord <- order(unique(UStornadoes$TornadoesSqMile), decreasing=T)
stateOrder <- data.frame(state = unique(UStornadoes$state)[ord], rank = 1:49)
UStornadoes$state <-
factor(UStornadoes$state, levels=stateOrder$state, ordered=TRUE)
UStornadoes$weight <- 1/UStornadoes$LandArea
USpolygons <- map_data("state")
USpolygons$state = state.abb[match(USpolygons$region, tolower(state.name))]
UStornadoCounts <-
ddply(UStornadoes, .(state, year), summarize, count=length(state))
seg.color <- "#55B1F7"
years <- data.frame(year=unique(UStornadoes$year))
states <- data.frame(state=unique(UStornadoes$state))
tornado.viz <- list(
map=ggplot()+
theme_bw()+
theme_animint(width=750, height=500)+
geom_text(aes(
-100, 50, label=paste(
"Tornado paths and endpoints in ", year),
showSelected=year),
data=years)+
geom_segment(aes(x=startLong, y=startLat, xend=endLong, yend=endLat,
showSelected=year),
colour=seg.color, data=UStornadoes)+
geom_point(aes(endLong, endLat, showSelected=year),
colour=seg.color,
fill=NA,
data=UStornadoes)+
geom_polygon(aes(x=long, y=lat, group=group, clickSelects=state),
data=USpolygons, fill="grey", colour="black", alpha=3/4)+
theme(axis.line=element_blank(), axis.text=element_blank(),
panel.background=element_rect(color=NA),
panel.border=element_rect(color=NA),
axis.ticks=element_blank(), axis.title=element_blank()),
ts=ggplot()+
theme_bw()+
theme_animint(width=300, height=400)+
xlab("year")+
ylab("Number of tornadoes")+
geom_bar(aes(year, count, clickSelects=year, showSelected=state),
data=UStornadoCounts, stat="identity", position="identity")+
geom_text(aes(
1980, 200, label=paste("state =", state), showSelected=state),
data=states)+
geom_text(aes(year, count + 5, label=count,
showSelected2=year, showSelected=state),
data=UStornadoCounts, size=20))
structure(tornado.viz, class="animint")
library(lubridate)
data(climate)
climate$time2 <- decimal_date(ymd(as.character(climate$date)))
countries <- map_data("world")
# Map coordinate limits chosen so that the polygons displayed are at
# least reasonably complete.
countries <- subset(countries, (lat < 38)&(lat>-24))
countries <- subset(countries, ((-long)>54)&((-long)<118))
# Create variable showing temp-avg.monthly.temp at that location
climate <- ddply(climate, .(id, month), transform,
tempdev = temperature - mean(temperature),
surfdev = surftemp - mean(surftemp))
climate <- climate[order(climate$date, climate$id), ]
# data frame with formatted labels
dates <- ddply(climate, .(date), summarise, month=month[1], year = year[1],
time2 = time2[1], textdate = paste(month.name[month], year))
dates <- dates[order(dates$date),]
climate <- climate[order(climate$time2),]
long.names <- c(surftemp="Surface temperature",
temperature="Temperature",
surfdev="Deviation from monthly norm")
lims <- list(surftemp=c(-10, 40),
surfdev=c(-8, 8))
var.names <- c("surftemp", "surfdev")
dot.alpha <- 6/10
viz <- list(duration=list(time2=3000),
time=list(variable="time2", ms=5000))
getlab <- function(var.name){
sprintf("%s (degrees Celsius)", long.names[[var.name]])
}
for(var.name in var.names){
long.name <- long.names[[var.name]]
viz[[sprintf("%sMap", var.name)]] <- ggplot() +
ggtitle(long.name)+
theme_bw()+
geom_tile(aes_string(x="long", y="lat", fill=var.name,
key="id",
clickSelects="id", showSelected="time2"),
data=climate)+
scale_fill_gradient2("deg. C", low="blue", mid="white", high="red",
midpoint=0, limits=lims[[var.name]]) +
geom_path(aes(long, lat, group=group), col="grey",
data=countries) +
geom_text(aes(-86, 39, label=textdate, key=time2, showSelected=time2),
data=dates)+
theme(axis.line=element_blank(), axis.text=element_blank(),
panel.background=element_rect(color=NA),
panel.border=element_rect(color=NA),
axis.ticks=element_blank(), axis.title=element_blank())
}
selected.color <- "violet"
viz$scatterNow <- ggplot()+
theme_bw()+
geom_text(aes(20, -7, label=sprintf("all regions in %s", textdate),
key=time2,
showSelected=time2),
data=dates)+
geom_hline(yintercept=0, color="grey")+
geom_vline(xintercept=0, color="grey")+
xlim(-10, 40)+
ylim(-8, 8)+
xlab(getlab(var.names[[1]]))+
ylab(getlab(var.names[[2]]))+
geom_point(aes_string(x=var.names[[1]], y=var.names[[2]],
key="id",
clickSelects="id", showSelected="time2"),
data=climate, alpha=dot.alpha)
times <- data.frame(time2=sort(unique(climate$time2)))
time.diff <- mean(diff(times$time2))/2
for(var.name in var.names){
long.name <- long.names[[var.name]]
viz[[sprintf("%sTimeSeries", var.name)]] <- ggplot()+
theme_bw()+
theme_animint(width=463)+
geom_hline(yintercept=0, color="grey")+
geom_tallrect(aes(
xmin=time2-time.diff, xmax=time2+time.diff, clickSelects=time2),
data=times,
alpha=0.5)+
xlab("Year of measurement")+
ylab(getlab(var.name))+
geom_line(aes_string(x="time2", y=var.name,
group="id", clickSelects="id"),
data=climate, alpha=53/100)
}
ids <- data.frame(id=unique(climate$id))
viz$scatterHere <- ggplot()+
theme_bw()+
geom_text(aes(
20, -7, label=paste("all times for region", id), showSelected=id),
data=ids)+
xlab(getlab(var.names[[1]]))+
ylab(getlab(var.names[[2]]))+
geom_hline(yintercept=0, color="grey")+
geom_vline(xintercept=0, color="grey")+
xlim(-10, 40)+
ylim(-8, 8)+
geom_point(aes_string(x=var.names[[1]], y=var.names[[2]],
clickSelects="time2", showSelected="id"),
data=climate, alpha=dot.alpha)
structure(viz, class="animint")
library(tourr)
mat <- rescale(as.matrix(flea[1:6]))
tour <- new_tour(mat, grand_tour(), NULL)
tour_dat <- function(step_size) {
step <- tour(step_size)
proj <- center(mat %*% step$proj)
data.frame(x = proj[,1], y = proj[,2],
species = flea$species)
}
steps <- c(0, rep(1/15, 200))
stepz <- cumsum(steps)
dats <- lapply(steps, tour_dat)
datz <- Map(function(x, y) cbind(x, step = y), dats, stepz)
dat <- do.call("rbind", datz)
viz.tour <- list(
plot = ggplot() +
geom_point(data = dat,
aes(x = x, y = y, colour = species, showSelected = step)),
time = list(variable = "step", ms = 100),
duration = list(step = 200)
)
structure(viz.tour, class="animint")
#> Warning in animint2dir(x, out.dir = dir, json.file = "plot.json", open.browser = FALSE): to ensure that smooth transitions are interpretable, aes(key) should be specifed for geoms with
#> aes(showSelected=step), problem: geom1_point_plot
devtools::session_info()
#> setting value
#> version R version 3.3.3 (2017-03-06)
#> system x86_64, linux-gnu
#> ui X11
#> language en_CA:en
#> collate en_CA.UTF-8
#> tz America/Montreal
#> date 2018-03-12
#>
#> package * version date source
#> animint * 2016.08.21 2017-03-10 Github (tdhock/animint@78974d8)
#> backports 1.1.0 2017-05-22 cran (@1.1.0)
#> colorspace 1.2-6 2015-03-11 CRAN (R 3.2.3)
#> devtools 1.12.0.9000 2016-08-12 Github (hadley/devtools@565ac15)
#> digest 0.6.12 2017-01-27 cran (@0.6.12)
#> evaluate 0.10.1 2017-06-24 CRAN (R 3.3.3)
#> ggplot2 * 2.1.0 2017-03-28 Github (faizan-khan-iit/ggplot2@5fb99d0)
#> gtable 0.2.0 2016-02-26 CRAN (R 3.2.3)
#> htmltools 0.3.6 2017-09-11 Github (rstudio/htmltools@02678ee)
#> knitr 1.19 2018-01-29 CRAN (R 3.3.3)
#> labeling 0.3 2014-08-23 CRAN (R 3.2.3)
#> lubridate * 1.3.3 2013-12-31 CRAN (R 3.0.2)
#> magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
#> maps * 3.0.2 2016-01-04 CRAN (R 3.2.3)
#> memoise 1.0.0 2016-01-29 CRAN (R 3.2.3)
#> munsell 0.4.3 2016-02-13 CRAN (R 3.2.3)
#> plyr * 1.8.4 2016-06-08 CRAN (R 3.2.3)
#> Rcpp 0.12.12 2017-07-15 cran (@0.12.12)
#> RJSONIO 1.3-1 2016-08-03 local
#> rmarkdown 1.8 2017-11-17 CRAN (R 3.3.3)
#> rprojroot 1.2 2017-01-16 cran (@1.2)
#> scales 0.5.0 2017-08-24 cran (@0.5.0)
#> stringi 1.1.5 2017-04-07 cran (@1.1.5)
#> stringr 1.2.0 2017-02-18 CRAN (R 3.3.3)
#> tourr * 0.5.5 2017-08-02 CRAN (R 3.3.3)
#> withr 1.0.2 2016-06-20 CRAN (R 3.2.3)
#> yaml 2.1.14 2016-11-12 cran (@2.1.14)