Graph

This code creates the picture from the starting page:

library(ggplot2)
library("RColorBrewer")

n <- 250
id <- seq(1:n)

# Select points out of grid
points <- matrix(NA, nrow=0, ncol=2)
xn <- 640
yn <- 400
for (i in 1:xn){points <- rbind(points, cbind(rep(i, yn), seq(1:yn)))}
selection <- sample(seq(1:(xn*yn)), n, replace=F)
x <- points[selection,1]
y <- points[selection,2]
z <- dnorm((y-yn/2), 0, yn) # size of circle
z <- exp(z / max(z))^2.4 # adjustment of circle-size
d <- sqrt(((1/4*xn - x))^2 + (5/12*yn - y)^2) # euclidean distance from first infected
d <- d / max(d)
#ul <- sqrt(((1 - x)/10)^2 + (1 - y)^2) # distance from bottom-left corner

data <- as.data.frame(cbind(id,x,y,z,d))
#data <- data[order(ul),]

# Idea: sample lines with probability from inverse distance matrix
data1 <- matrix(0, nrow=n, ncol=n)
for (i in 1:n){
for (j in i:n){
data1[j,i] <- sqrt(((data$x[i] - data$x[j]))^2 + (data$y[i] - data$y[j])^2)
}
}
diag(data1) <- Inf
data1 <- mirror.tri(data1)

data2 <- data1
# Index of nearest neighbours
index <- data$id[1]
for (i in 2:(dim(data)[1])){
index[i] <- data$id[which(data1[index[i-1],] == min(data1[index[i-1],]))]
data1[,index[i-1]] <- Inf
data1[index[i-1],] <- Inf
}
index <- as.data.frame(index)
colnames(index) <- "id"

# Transition Probabilites
for (i in 1:dim(data2)[2]){
  data2[i, which(data2[i,] > quantile(data2[i,], 0.25) | data2[i,] < quantile(data2[i,], 0.05))] <- 0
  data2[i, which(data2[i,] > 0)] <- dnorm(data2[i, which(data2[i,] > 0)], min(data2[i, which(data2[i,] > 0)]), 1) / sum(dnorm(data2[i, which(data2[i,] > 0)], min(data2[i, which(data2[i,] > 0)]), 1))
}

rtpois <- function(N, lambda) qpois(runif(N, dpois(0, lambda), 1), lambda)
data <- merge(index, data, by="id", sort=F)
data3 <- NA
for (i in 1:(dim(data)[1])){
for (j in 1:rtpois(1, 4)){
data3 <- rbind(data3, data$id[i])
data3 <- rbind(data3, sample(seq(1:n), 1, prob = data2[data$id[i],]))
data3 <- rbind(data3, data$id[i])
}
}

data3 <- as.data.frame(data3[2:length(data3)])
colnames(data3) <- 'id'
data3$sort <- seq(1:dim(data3)[1])
data3 <- merge(data3, data, by="id", sort=F)
data3 <- data3[order(data3$sort),]

p <- ggplot(data=data, aes(x=x, y=y))
p <- p + geom_path(data=data3, aes(colour=d))
p <- p + geom_jitter(aes(fill=d, colour=d), size=z, shape=21, stroke=1.5)
p <- p + scale_fill_gradientn(colours = c("#FF00CC", "grey90", "grey90", "grey90", "grey90"))
p <- p + scale_colour_gradientn(colours = c("#CC0099", "grey60", "grey60", "grey60", "grey60"))
p <- p + theme(panel.background = element_rect(fill = "white"), panel.grid = element_blank(), text=element_blank(), axis.ticks=element_blank(), title=element_blank(), legend.position="none")
p


%d bloggers like this: