## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 5, fig.height = 5 ) ## ----setup-------------------------------------------------------------------- library(gghinton) library(ggplot2) ## ----quick-start-------------------------------------------------------------- # A 10x10 signed matrix set.seed(99) m <- matrix(rnorm(100), nrow = 10) rownames(m) <- paste0("r", 1:10) colnames(m) <- paste0("c", 1:10) df <- matrix_to_hinton(m) ggplot(df, aes(x = col, y = row, weight = weight)) + geom_hinton() + scale_fill_hinton() + coord_fixed() + theme_hinton() + labs(title = "A signed Hinton diagram") ## ----unsigned----------------------------------------------------------------- m_pos <- abs(m) df_pos <- matrix_to_hinton(m_pos) ggplot(df_pos, aes(x = col, y = row, weight = weight)) + geom_hinton() + scale_fill_hinton() + coord_fixed() + theme_hinton() + labs(title = "An unsigned Hinton diagram") ## ----as-hinton-df------------------------------------------------------------- # matrix as_hinton_df(matrix(c(1, -2, 3, -4), 2, 2)) # base R table t2 <- table( group = c("A", "A", "B", "B"), outcome = c("yes", "no", "yes", "no") ) as_hinton_df(t2) ## ----as-hinton-df-df---------------------------------------------------------- tidy <- data.frame(row = c(1, 1, 2, 2), col = c(1, 2, 1, 2), weight = c(0.5, -0.3, 0.8, -0.1)) as_hinton_df(tidy) ## ----scale-by, fig.width = 8, fig.height = 4---------------------------------- set.seed(1) df_a <- cbind(matrix_to_hinton(matrix(runif(9, -1, 1), 3, 3)), panel = "A (range +/-1)") df_b <- cbind(matrix_to_hinton(matrix(runif(9, -5, 5), 3, 3)), panel = "B (range +/-5)") df_ab <- rbind(df_a, df_b) # Per-panel scaling: each panel's largest value fills its cell ggplot(df_ab, aes(x = col, y = row, weight = weight)) + geom_hinton(scale_by = "panel") + scale_fill_hinton() + coord_fixed() + theme_hinton() + facet_wrap(~panel) + labs(title = 'scale_by = "panel" (default)') # Global scaling: panel A appears much smaller ggplot(df_ab, aes(x = col, y = row, weight = weight)) + geom_hinton(scale_by = "global") + scale_fill_hinton() + coord_fixed() + theme_hinton() + facet_wrap(~panel) + labs(title = 'scale_by = "global"') ## ----custom-colours----------------------------------------------------------- df <- matrix_to_hinton(m) ggplot(df, aes(x = col, y = row, weight = weight)) + geom_hinton(background = FALSE) + scale_fill_hinton(values = c(positive = "darkblue", negative = "darkred")) + coord_fixed() + theme_hinton() + labs(title = "Custom colours") ## ----axis-labels-------------------------------------------------------------- # m has rownames/colnames set above df_named <- matrix_to_hinton(m) ggplot(df_named, aes(x = col, y = row, weight = weight)) + geom_hinton() + scale_fill_hinton() + scale_x_continuous( breaks = seq_len(ncol(m)), labels = colnames(m) ) + scale_y_continuous( breaks = seq_len(nrow(m)), labels = rev(rownames(m)) # reversed because row 1 is at the top ) + coord_fixed() + theme_hinton() + labs(title = "Named axes") ## ----no-bg-------------------------------------------------------------------- ggplot(df, aes(x = col, y = row, weight = weight)) + geom_hinton(background = FALSE) + scale_fill_hinton(values = c(positive = "grey70")) + coord_fixed() + theme_hinton() + labs(title = "Signed data without background") ## ----mtcars-cor, fig.width = 6, fig.height = 6-------------------------------- df_cor <- as_hinton_df(cor(mtcars)) vars <- colnames(mtcars) ggplot(df_cor, aes(x = col, y = row, weight = weight)) + geom_hinton() + scale_fill_hinton() + scale_x_continuous(breaks = seq_along(vars), labels = vars) + scale_y_continuous(breaks = seq_along(vars), labels = rev(vars)) + coord_fixed() + theme_hinton() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "cor(mtcars)", subtitle = "White = positive, black = negative correlation")