Skip to content

Instantly share code, notes, and snippets.

@dgopstein
Last active January 11, 2019 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgopstein/0a2220c89537403e2cfa6a5b09ab2dfd to your computer and use it in GitHub Desktop.
Save dgopstein/0a2220c89537403e2cfa6a5b09ab2dfd to your computer and use it in GitHub Desktop.
Spot Matrix in R with ggplot2
##################################
# Data
##################################
ops.mat <-
matrix(c(392, 114, 250, 115, 115, 78,
2349, 560, 428, 286, 226, 173,
4037, 1215, 817, 879, 479, 337,
966, 592, 142, 256, 90, 407),
nrow=4, byrow=TRUE)
project.node.totals <- c(28047, 79005, 140853, 29529)
rownames(ops.mat) <- c("go", "c", "cpp", "fortran")
colnames(ops.mat) <- c("a = b","a == b","!a","a && b","a != b","&a")
scale.max.1 <- function(x) x / max(x)
ops.rate.mat <- scale.max.1(apply(ops.mat, 2, '/', project.node.totals))
ops.rate.df <- data.frame(reshape2::melt(ops.rate.mat, varnames=c("parser", "op"), value.name = "rate"))
##################################
# Includes
##################################
install.packages(c("ggplot2", "bertin", "pals", "gridExtra"))
library(ggplot2)
##################################
# Table
##################################
Hmisc::latex(ops.mat, title='')
##################################
# Bertin Matrix
##################################
bertin::plot.bertin(ops.mat, main='', palette = c("black", "black"), showpalette = FALSE)
##################################
# Heatmap
##################################
heatmap(ops.mat, Colv=NA, Rowv=NA, col=pals::parula(64))
##################################
# Spot Matrix
##################################
ggplot(data=ops.rate.df, aes(op, parser)) + geom_point(aes(size = rate))
##################################
# Spot Matrix Theme
##################################
spot.theme <- list(
theme_classic(),
theme(axis.ticks.x=element_blank(), axis.text.x=element_text(size = 19, angle = 90, hjust = 0)),
theme(axis.ticks.y=element_blank(), axis.text.y=element_text(size = 19)),
theme(axis.line=element_blank()),
theme(text = element_text(size = 22)),
theme(legend.position = "none"),
theme(plot.margin = unit(c(10,10,10,10), "mm")),
scale_size_continuous(range = c(-0.3, 15)),
scale_x_discrete(position = "top"))
# Themed plot
ggplot(data=ops.rate.df, aes(op, parser)) + geom_point(aes(size = rate)) + spot.theme
##################################
# Spot Matrix Variation Grid
##################################
colors <- pals::parula(10)[c(1,4,7,9)]
plain.spot <- ggplot(ops.rate.df, aes(op, parser)) + spot.theme + ggtitle("Plain Spot") +
geom_point(colour = colors[[1]], aes(size = rate))
donut.spot <- ggplot(ops.rate.df, aes(op, parser)) + spot.theme + ggtitle("Donut Spot") +
geom_point(colour = colors[[2]], aes(size = 1)) +
geom_point(colour = "white", aes(size = scale.max.1(1/rate)))
center.spot <- ggplot(ops.rate.df, aes(op, parser)) + spot.theme + ggtitle("Center Spot") +
geom_point(colour = "gray20", aes(size = 1)) +
geom_point(colour = colors[[3]], aes(size = rate))
ring.spot <- ggplot(ops.rate.df, aes(op, parser)) + spot.theme + ggtitle("Ring Spot") +
geom_point(colour = "black", aes(size = 1)) +
geom_point(colour = "white", aes(size = 0.8)) +
geom_point(colour = colors[[4]], aes(size = 0.81*rate))
spot.grid <- gridExtra::grid.arrange(plain.spot, donut.spot, center.spot, ring.spot, nrow=2)
##################################
# Colored Groups
##################################
op.type <- factor(c(1,2,2,2,2,3), labels=c("assignment", "logical", "address"))
ggplot(ops.rate.df, aes(op, parser)) + spot.theme +
geom_point(colour = "black", aes(size = 1)) +
geom_point(colour = "white", aes(size = 0.8)) +
geom_point(aes(colour = op.type[op], size = 0.81*rate)) +
scale_colour_manual(values = colors[2:4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment