m.list <- list(
island=rbind(
c(0,0,0,0,0,0,0),
c(0,1,1,1,1,1,0),
c(0,1,0,0,0,1,0),
c(0,1,0,1,0,1,0),
c(0,1,0,0,0,1,0),
c(0,1,1,1,1,1,0),
c(0,0,0,0,0,0,0)),
hole=rbind(
c(0,0,0,0,0,0,0),
c(0,1,1,1,1,1,0),
c(0,1,0,0,0,1,0),
c(0,1,0,0,0,1,0),
c(0,1,0,0,0,1,0),
c(0,1,1,1,1,1,0),
c(0,0,0,0,0,0,0)),
filled=rbind(
c(0,0,0,0,0,0,0),
c(0,1,1,1,1,1,0),
c(0,1,1,1,1,1,0),
c(0,1,1,1,1,1,0),
c(0,1,1,1,1,1,0),
c(0,1,1,1,1,1,0),
c(0,0,0,0,0,0,0)))
poly.list <- list()
point.list <- list()
for(grp.i in seq_along(m.list)){
offset <- grp.i * 7
group <- names(m.list)[[grp.i]]
m <- m.list[[grp.i]]
clines <- contourLines(1:ncol(m), 1:nrow(m), m, levels=0.5)
poly.list[[grp.i]] <- data.table(
subgroup=seq_along(clines)
)[, with(clines[[subgroup]], data.table(
group, x=x+offset, y
)), by=subgroup]
point.list[[grp.i]] <- data.frame(
row=as.integer(row(m)),
col=as.integer(col(m)) + offset,
num=factor(as.numeric(m), levels=c(0, 1)),
group=group)
}
poly.df <- do.call(rbind, poly.list)
point.df <- do.call(rbind, point.list)
animint(
contours=ggplot()+
scale_fill_manual(
values=c("0"="white", "1"="black"),
breaks=c("0", "1"))+ # only two data values
geom_point(aes(
col, row, fill=num),
data=point.df,
color="red", size=3)+
geom_polygon(aes(
x, y, group=group, subgroup=subgroup, tooltip=group),
data=poly.df,
alpha=0.5,
fill="steelblue")+
xlab("Three contour polygons: island (left), hole (middle), filled (right)")+
theme_animint(width=800, height=300)
)