Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created July 19, 2024 09:20
Show Gist options
  • Save abikoushi/95b4ea8086881409c2c649736e9768df to your computer and use it in GitHub Desktop.
Save abikoushi/95b4ea8086881409c2c649736e9768df to your computer and use it in GitHub Desktop.
sparkline with dendrogram
plot.miniLineTable <- function(tab,...){
oldpar <- graphics::par(no.readonly = TRUE)
N <- length(tab$y)
graphics::par(mar = c(4, 10, 0, 2), oma = rep(1, 4))
graphics::layout(mat = cbind(N+1,1:N,1:N), respect = FALSE)
for (i in 1:N) {
tmpy <- tab$y[[i]]
matplot(tmpy, type = "l", lty=1, xaxt="n", yaxt="n",
xlab = "", ylab = "", frame.plot = FALSE,
col=1)
ran <-range(tmpy)
graphics::axis(side=4, at = format(ran, digits = 2), las=2)
graphics::mtext(names(tab$y[i]),side=2,las=2)
}
m <- sapply(tab$y, rowMeans)
h <- as.dendrogram(hclust(dist(t(m)), method = "ward.D"))
plot(h, sub = "", main = "", horiz=TRUE,leaflab = "none")
graphics::axis(side=1,at=1:nrow(tmpy),labels = tab$x,lwd=0)
graphics::par(oldpar)
}
set.seed(2)
y <- lapply(1:7, function(x){
k <- rpois(1,2)+1
matrix(rnorm(6*k),6,k)
})
names(y) <- sapply(1:7, function(x)paste0(sample(LETTERS,10),collapse = ""))
tab <- list(y=y,x=1:6)
class(tab) <- "miniLineTable"
#png("miniLine.png")
plot(tab)
#dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment